php foreach 继续

发布于 2024-10-04 10:04:53 字数 716 浏览 9 评论 0原文

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

我哪里出错了?

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

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉城已无爱 2024-10-11 10:04:54

您在 for 循环中调用 continue,因此 continue 将为 for 循环执行,而不是 foreach一。使用:

continue 2;

You are calling continue in a for loop, so continue will be done for the for loop, not the foreach one. Use:

continue 2;
可爱暴击 2024-10-11 10:04:54

for 循环中的 continue 将在 for 循环中跳过,而不是在 foreach 循环中。

The continue within the for loop will skip within the for loop, not the foreach loop.

你与清晨阳光 2024-10-11 10:04:53

如果您尝试将第二个 Continue 应用于 foreach 循环,则必须将其从 更改

continue;

continue 2;

这将指示 PHP 应用 Continue code> 语句添加到第二个嵌套循环,即 foreach 循环。否则,它将仅适用于 for 循环。

If you are trying to have your second continue apply to the foreach loop, you will have to change it from

continue;

to

continue 2;

This will instruct PHP to apply the continue statement to the second nested loop, which is the foreach loop. Otherwise, it will only apply to the for loop.

死开点丶别碍眼 2024-10-11 10:04:53

第二个继续位于另一个循环中。这只会“重新启动”内部循环。如果你想重新启动外层循环,你需要给继续一个提示,它应该上升多少次循环

continue 2;

参见手册

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

continue 2;

See Manual

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文