gpt4 book ai didi

php - foreach 不会在中断时停止循环

转载 作者:行者123 更新时间:2023-12-02 15:20:50 26 4
gpt4 key购买 nike

我尝试使用 foreach 发布 3 个数组,由于某种原因,最后的 break 不起作用,它输出整个列表(40+)到页面。

$file = fopen('names.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
//$line is an array of the csv elements

shuffle($line);

$i = 0;

foreach ($line as $number) {
{
if($i==3){ break; } else {
$rtime = mt_rand(1, 7);
echo $number; }

$i++;
}

}


}
fclose($file);

它看起来是这样的:take.ms/cLgIh,相反,它应该只显示其中的 3 个用户名。

最佳答案

<?php
$i = 0;
//I have opened my contact.csv :P
$file = fopen('contact.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
//$line is an array of the csv elements
shuffle($line);

foreach ($line as $number) {
{
if($i==3){ exit(); } else {
$rtime = mt_rand(1, 7);
echo "<br/> i = ".$i.$number.", "; }
}
$i++;
}

}
fclose($file);
?>

关于php - foreach 不会在中断时停止循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39670936/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com