正如标题所说:我有一个Foreach循环,但是每三次循环都会通过,它必须做其他事情。如何做到这一点?提前感谢。我试过:
$count = 0
foreach ($cObjects as $oObject) {
if ($count <= 2) {
//do things
$count = 0;
} else {
// do other things
}
$count++;
}
这当然不起作用,而它每次都将计数重置为0。
替换if($count
Parixit的答案只适用于索引数组,对于集合或关联数组,您将需要一个单独的$count变量,类似于以下内容:
$count = 0
foreach ($cObjects as $oObject) {
if ($count == 2) {
//do things
$count = 0;
} else {
// do other things
}
$count++;
}
我不知道我理解得是否正确。但你应该做这样的事
foreach($all as $k=>$single) {
if($k%3==0) {
//do every third time
continue;
}
//do something which will not execute every third time
}