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 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用