php foreach 继续
如果不满足某些条件,我尝试跳到循环的下一次迭代。问题是无论如何循环都会继续。
我哪里出错了?
更新了代码示例以响应第一条评论。
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您在
for
循环中调用continue
,因此 continue 将为for
循环执行,而不是foreach
一。使用:You are calling
continue
in afor
loop, so continue will be done for thefor
loop, not theforeach
one. Use:for
循环中的continue
将在for
循环中跳过,而不是在foreach
循环中。The
continue
within thefor
loop will skip within thefor
loop, not theforeach
loop.如果您尝试将第二个
Continue
应用于foreach
循环,则必须将其从 更改为
这将指示 PHP 应用
Continue
code> 语句添加到第二个嵌套循环,即foreach
循环。否则,它将仅适用于for
循环。If you are trying to have your second
continue
apply to theforeach
loop, you will have to change it fromto
This will instruct PHP to apply the
continue
statement to the second nested loop, which is theforeach
loop. Otherwise, it will only apply to thefor
loop.第二个继续位于另一个循环中。这只会“重新启动”内部循环。如果你想重新启动外层循环,你需要给继续一个提示,它应该上升多少次循环
参见手册
The second continue is in another loop. This one will only "restart" the inner loop. If you want to restart the outer loop, you need to give continue a hint how much loops it should go up
See Manual