php foreach 继续
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4270102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php foreach continue
提问by JasonS
I am trying to skip to the next iteration of the loop if certain conditions are not met. The problem is that the loop is continuing regardless.
如果不满足某些条件,我会尝试跳到循环的下一次迭代。问题是循环仍在继续。
Where have I gone wrong?
我哪里错了?
Updated Code sample in response to first comment.
更新代码示例以响应第一条评论。
foreach ($this->routes as $route => $path) {
$continue = 0;
...
// Continue if route and segment count do not match.
if (count($route_segments) != $count) {
$continue = 12;
continue;
}
// Continue if no segment match is found.
for($i=0; $i < $count; $i++) {
if ($route_segments[$i] != $segments[$i] && ! preg_match('/^\x24[0-9]+$/', $route_segments[$i])) {
$continue = 34;
continue;
}
}
echo $continue; die(); // Prints out 34
回答by cdhowie
If you are trying to have your second continueapply to the foreachloop, you will have to change it from
如果您尝试将第二个continue应用于foreach循环,则必须将其从
continue;
to
到
continue 2;
This will instruct PHP to apply the continuestatement to the second nested loop, which is the foreachloop. Otherwise, it will only apply to the forloop.
这将指示 PHP 将该continue语句应用于第二个嵌套循环,即foreach循环。否则,它将仅适用于for循环。
回答by KingCrunch
回答by netcoder
You are calling continuein a forloop, so continue will be done for the forloop, not the foreachone. Use:
你调用continue一个for循环,所以继续为会做for循环,而不是foreach一个。用:
continue 2;
回答by BeemerGuy
The continuewithin the forloop will skip within the forloop, not the foreachloop.
在continue该范围内for环路将内跳过for循环,而不是foreach循环。

