Php forloop在迭代结束后再次继续
我使用的函数与下面给出的逻辑相同 下面给出的代码是我使用的相同逻辑的示例,我需要在迭代结束后继续for循环,我在末尾将0赋给变量$j,所以forloop需要继续,为什么它关闭了这个过程Php forloop在迭代结束后再次继续,php,for-loop,Php,For Loop,我使用的函数与下面给出的逻辑相同 下面给出的代码是我使用的相同逻辑的示例,我需要在迭代结束后继续for循环,我在末尾将0赋给变量$j,所以forloop需要继续,为什么它关闭了这个过程 for($i=$j;$i<7;$i++){ echo "<br/>".$i; if($i == 6){$j=0;continue;} } 预期产量 1 2 3 4 5 6 1 2 3 4 5 6 .....etc 我的原始代码示例是 foreach($Qry_Rst as $key=
for($i=$j;$i<7;$i++){
echo "<br/>".$i;
if($i == 6){$j=0;continue;}
}
预期产量
1
2
3
4
5
6
1
2
3
4
5
6
.....etc
我的原始代码示例是
foreach($Qry_Rst as $key=>$Rst_Val){
for($j=$ItrDt;$j<7;$j++){
$ItrDate = date('Y-m-d', mktime(0, 0, 0, $month, $day + $j, $year));
if($ItrDate == $Rst_Val['sloat_day']){
$TimeTableAry[$loop_itr] = $Rst_Val;
break
}
}
}
foreach($Qry\u Rst as$key=>$Rst\u Val){
对于($j=$ItrDt;$j
第一个表达式(expr1)在循环开始时无条件计算(执行)一次
因此,使用$i
重置循环,而不是$j
将循环隔离到它自己的函数中将允许一行执行所需的命令,从而允许主代码类似()
$data=array(数组(“foo”、“bar”)、数组(“hello”)、数组(“world”、“!”);
函数justDump($obj){
var_dump($obj);
};
$i=0;
做{
$data=doFor($data,'justDump');
打印“
”;
$i++;
}而($i您也可以尝试您最初想要的方式(仅稍微编辑):
//避免无限循环的计数器
$counter=0;
对于($i=$i;$i)
foreach($Qry_Rst as $key=>$Rst_Val){
for($j=$ItrDt;$j<7;$j++){
$ItrDate = date('Y-m-d', mktime(0, 0, 0, $month, $day + $j, $year));
if($ItrDate == $Rst_Val['sloat_day']){
$TimeTableAry[$loop_itr] = $Rst_Val;
break
}
}
}
$j = 1;
$current = 0;
for ($i=$j; $i<4; $i++) {
printf("i: %d, j: %d\n", $i, $j);
if ($i==3 && $current < 5) {
$i = -1;
$j = mt_rand(0,3);
$current++;
continue;
}
}
function doFor($data, $callback) {
$dataLength = count($data);
for ($i=0; $i<$dataLength; $i++) {
call_user_func($callback, $data[$i]);
}
return $data;
}
$data = array(array("foo","bar"),array("hello"),array("world","!"));
function justDump($obj) {
var_dump($obj);
};
$i = 0;
do {
$data = doFor($data, 'justDump');
print "<br>";
$i++;
} while($i<5);
//counter to avoid infinite loop
$counter = 0;
for($i=$i;$i<7;$i++){
echo "<br/>".$i;
if($i == 6){
$i=0;
$counter++;
continue;
}
if($counter == 5){break;}
}
1
2
3
4
5
6
1
2
3
...