‘打破’从开关,然后“继续”循环中

发布于 2024-12-16 20:27:42 字数 551 浏览 1 评论 0原文

是否可以从开关中断然后继续循环?

例如:

$numbers= array(1,2,3,4,5,6,7,8,9,0);
$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g');

foreach($letters as $letter) {
    foreach($numbers as $number) {
        switch($letter) {
           case 'd':
               // So here I want to 'break;' out of the switch, 'break;' out of the
               // $numbers loop, and then 'continue;' in the $letters loop.
               break;
        }
    }

    // Stuff that should be done if the 'letter' is not 'd'.

}

这可以做到吗?语法是什么?

Is it possible to break from a switch and then continue in a loop?

For example:

$numbers= array(1,2,3,4,5,6,7,8,9,0);
$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g');

foreach($letters as $letter) {
    foreach($numbers as $number) {
        switch($letter) {
           case 'd':
               // So here I want to 'break;' out of the switch, 'break;' out of the
               // $numbers loop, and then 'continue;' in the $letters loop.
               break;
        }
    }

    // Stuff that should be done if the 'letter' is not 'd'.

}

Can this be done, and what would the syntax be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

热风软妹 2024-12-23 20:27:42

您想使用 break n

break 2;

澄清后,看起来您想要 继续 2;

You want to use break n

break 2;

After clarification, looks like you want continue 2;

岁月流歌 2024-12-23 20:27:42

使用继续2而不是break

Instead of break, use continue 2.

无语# 2024-12-23 20:27:42

我知道这是一个严重的死灵,但是……当我从谷歌来到这里时,我想我可以避免其他人的困惑。

如果他的意思是从开关中退出并只是结束数字循环,那么 break 2; 就可以了。 continue 2; 只会继续数字的循环并继续迭代它,以便每次都继续

因此,正确的答案应该是 continue 3;

通过 文档中的评论继续基本上会转到结构的末尾,对于 switch 来说就是这样(感觉与break相同),对于循环它会在下一次迭代中拾取。

请参阅:http://codepad.viper-7.com/dGPpeZ

上述情况的示例 n/ a:

<?php
    echo "Hello, World!<pre>";

$numbers= array(1,2,3,4,5,6,7,8,9,0);
$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');

$i = 0;
foreach($letters as $letter) {
    ++$i;
    echo $letter . PHP_EOL;
    foreach($numbers as $number) {
        ++$i;
        switch($letter) {
           case 'd':
               // So here I want to 'break;' out of the switch, 'break;' out of the
               // $numbers loop, and then 'continue;' in the $letters loop.
              continue 3; // go to the end of this switch, numbers loop iteration, letters loop iteration
            break;
           case 'f':
            continue 2; // skip to the end of the switch control AND the current iteration of the number's loop, but still process the letter's loop
            break;
           case 'h':
            // would be more appropriate to break the number's loop
            break 2;

        }
        // Still in the number's loop
        echo " $number ";
    }


    // Stuff that should be done if the 'letter' is not 'd'.
    echo " $i " . PHP_EOL;

}

结果:

Hello, World!
a
 1  2  3  4  5  6  7  8  9  0  11 
b
 1  2  3  4  5  6  7  8  9  0  22 
c
 1  2  3  4  5  6  7  8  9  0  33 
d
e
 1  2  3  4  5  6  7  8  9  0  46 
f
 57 
g
 1  2  3  4  5  6  7  8  9  0  68 
h
 70 
i
 1  2  3  4  5  6  7  8  9  0  81 

continue 2; 不仅处理字母 d 的字母循环,甚至还处理数字循环的其余部分(请注意,$i 既递增又打印f)之后。 (这可能是理想的,也可能不是理想的......)

希望能帮助其他首先到达这里的人。

I know this is a serious necro but... as I arrived here from Google figured I'd save others the confusion.

If he meant breaking out from the switch and just ending the number's loop, then break 2; would've been fine. continue 2; would just continue the number's loop and keep iterating through it just to be continue'd each time.

Ergo, the correct answer ought to be continue 3;.

Going by a comment in the docs continue basically goes to the end of the structure, for switch that's that (will feel the same as break), for loops it'd pick up on the next iteration.

See: http://codepad.viper-7.com/dGPpeZ

Example in case above n/a:

<?php
    echo "Hello, World!<pre>";

$numbers= array(1,2,3,4,5,6,7,8,9,0);
$letters = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i');

$i = 0;
foreach($letters as $letter) {
    ++$i;
    echo $letter . PHP_EOL;
    foreach($numbers as $number) {
        ++$i;
        switch($letter) {
           case 'd':
               // So here I want to 'break;' out of the switch, 'break;' out of the
               // $numbers loop, and then 'continue;' in the $letters loop.
              continue 3; // go to the end of this switch, numbers loop iteration, letters loop iteration
            break;
           case 'f':
            continue 2; // skip to the end of the switch control AND the current iteration of the number's loop, but still process the letter's loop
            break;
           case 'h':
            // would be more appropriate to break the number's loop
            break 2;

        }
        // Still in the number's loop
        echo " $number ";
    }


    // Stuff that should be done if the 'letter' is not 'd'.
    echo " $i " . PHP_EOL;

}

Results:

Hello, World!
a
 1  2  3  4  5  6  7  8  9  0  11 
b
 1  2  3  4  5  6  7  8  9  0  22 
c
 1  2  3  4  5  6  7  8  9  0  33 
d
e
 1  2  3  4  5  6  7  8  9  0  46 
f
 57 
g
 1  2  3  4  5  6  7  8  9  0  68 
h
 70 
i
 1  2  3  4  5  6  7  8  9  0  81 

continue 2; not only processes the letter's loop for the letter d but even processes the rest of the number's loop (note that $i is both incremented and printed after f). (Which may or may not be desirable...)

Hope that helps anyone else that winds up here first.

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