php foreach继续

Jas*_*onS 49 php foreach continue

如果不满足某些条件,我试图跳到循环的下一次迭代.问题是循环无论如何都在继续.

我哪里出错了?

更新了代码示例以响应第一条评论.

    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
Run Code Online (Sandbox Code Playgroud)

cdh*_*wie 107

如果您尝试将第二个continue应用于foreach循环,则必须从中进行更改

continue;
Run Code Online (Sandbox Code Playgroud)

continue 2;
Run Code Online (Sandbox Code Playgroud)

这将指示PHP将continue语句应用于第二个嵌套循环,即foreach循环.否则,它只适用于for循环.


Kin*_*nch 18

第二个继续是另一个循环.这个只会"重启"内循环.如果你想重新启动外部循环,你需要继续提示它应该增加多少循环

continue 2;
Run Code Online (Sandbox Code Playgroud)

手册


net*_*der 10

你调用continue一个for循环,所以继续为会做for循环,而不是foreach一个.使用:

continue 2;
Run Code Online (Sandbox Code Playgroud)