dongxie45083 2016-11-26 00:26
浏览 81
已采纳

来自txt文件中数组的in_array不适用于大于10的int

I've tried this

$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 contains in this example:

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

The only one solution for this script is 11 at the end, but it's 10 sometime too (I don't know why), it's sometime 10, but never 1, 2, 3 ... 9. It's not working for int greater than 10.

I've tried with this, but it's not even working too

$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);
  • 写回答

1条回答 默认 最新

  • doupo2157 2016-11-26 01:12
    关注

    Did you try to output the $tt-array? It contains this: Array ( [0] => " [1] => 8 [2] => " [3] => , [4] => [5] => " [6] => 9 [7] => " [8] => , [9] => [10] => " [11] => 1 [12] => 0 [13] => " [14] => , [15] => )

    No wonder 10 shows up sometimes... You need to fix how you make that array, first off you need to split the content properly, and remove spaces (use trim()), double quotes and commas.

    The following code works:

    <?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;
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上