大家好,我需要将foreach循环转换为while循环。因为foreach循环在值迭代后确实会离开块。我需要while循环来继续循环。
我需要迭代数组的项,但不是在foreach循环中。
foreach($values as $event) {
if($startDate >= $event['start'] && $startDate <= $event['end'] ) {
echo '<tr><td style="background:red;">Tijd: ' . strftime("== %H:%M ==", $startDate) . '<br/></td><td>'.$startDate.'</td></tr>';
}
else {
echo '<tr><td style="background:green;">Tijd: ' . strftime("== %H:%M ==", $startDate) . '<br/></td><td>'.$startDate.'</td></tr>';
}
}
我的上一篇文章是在一场灾难中发表的。
如果您只是想遍历数组,那么应该这样做:
$array_length = count($values);
$iteration = 0;
while($iteration < $array_length){
$event = $values[$iteration];
...
$iteration++;
}
这个功能很像for((或foreach((循环,如果你只想在满足特定条件时退出循环,你可以这样做:
$active = true;
$iteration = 0;
while($active){
$event = $values[$iteration];
...
if(some_condition){ $active = false; }
$iteration++;
}
注意:如果迭代器的大小大于或等于值数组的大小,您应该输入一些代码来重置迭代器或将$active设置为false,否则您将遇到麻烦