php foreach 繼續 (php foreach continue)


問題描述

php foreach 繼續 (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

## 參考解法 #### 方法 1:

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.

方法 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

方法 3:

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

continue 2;

方法 4:

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

(by JasonScdhowieKingCrunchnetcoderBeemerGuy)

參考文件

  1. php foreach continue (CC BY‑SA 3.0/4.0)

#continue #foreach #PHP






相關問題

skrip shell: loop bersarang dan lanjutkan (shell script : nested loop and continue)

syntaxError:“繼續”在循環中不正確 (syntaxError: 'continue' not properly in loop)

php continue - 替代方式? (php continue - alternative way?)

Python 中的歐拉 #4 (Euler #4 in Python)

if-continue vs 嵌套 if (if-continue vs nested if)

如何返回上一個 while 循環? (How do I return to the previous while loop?)

php foreach 繼續 (php foreach continue)

在while循環中繼續的奇怪行為 (weird behaviour of continue in while loop)

是否可以使用 'yield' 來生成 'Iterator' 而不是 Scala 中的列表? (Is it possible to use 'yield' to generate 'Iterator' instead of a list in Scala?)

BASH:在 for 循環中使用 continue (BASH: Using a continue in a for loop)

rand() 函數後繼續指令的問題 (Problem with continue instruction after rand() function)

在tictactoe遊戲中重複輸入錯誤 (Error in repeating input in a tictactoe game)







留言討論