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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:28:59  来源:igfitidea点击:

php foreach continue

phpforeachcontinue

提问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

The second continueis in another loop. This one will only "restart" the inner loop. If you want to restart the outer loop, you need to give continuea hint how much loops it should go up

第二个继续在另一个循环中。这只会“重新启动”内部循环。如果你想重新启动外循环,你需要给continue一个提示它应该增加多少循环

continue 2;

See Manual

参见手册

回答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循环。