2016-11-26 24 views
0

我已经试过这in_array不工作的INT大于十

$randtxt = fopen('random.txt','r'); 
$zawa = fread($randtxt, 8192); 
$tt = str_split($zawa); 


do { 

    $numer = rand(8, 11); 

} while (in_array($numer, $tt)); 


echo<<<END 
$numer 
END; 

Random.txt包含在这个例子:

"8", "9", "10", 

唯一一个解决方案,这个脚本最后是11,但也有10次(我不知道为什么),有时候是10,但从来没有1,2,3 ... 9.它不适用于大于10的int。

I我已经试过了,但它甚至不能工作

$randtxt = file_get_contents('random.txt'); 
$inarr = unserialize($randtxt); 

$number = 11;  

if (in_array($number, $inarr, true)) 
{ 
    echo "yup"; 
} 
else 
{ 
    echo "nope"; 
} 

    $numberout = serialize($number); 
    $out = file_put_contents('random.txt', $numberout, FILE_APPEND); 
+0

尝试[mt_rand(http://php.net/manual/en/function.mt-rand.php) – Thamilan

+0

你分割你的字符串转换成字符数组。您没有任何数组元素长于1个字符。所以每两个或更多的数字号码将不会被发现。 – Rizier123

回答

1

您是否尝试输出$tt -array?它包含了这一点: Array ([0] => " [1] => 8 [2] => " [3] => , [4] => [5] => " [6] => 9 [7] => " [8] => , [9] => [10] => " [11] => 1 [12] => 0 [13] => " [14] => , [15] =>)

难怪10有时显示了...你需要修复你如何作出这样的阵列,首先你需要的内容适当拆分,并删除空格(使用TRIM()),双引号和逗号。

下面的代码工作:

<?php 
$randtxt = file('random.txt'); 
$tt = str_replace(',','',str_replace('"','',explode(' ',$randtxt[0]))); 

do { 
    $numer = rand(8, 11); 
} while (in_array($numer, $tt)); 


echo<<<END 
$numer 
END; 

?>