【发布时间】:2011-11-20 22:55:02
【问题描述】:
是否可以从 switch 中中断然后继续循环?
例如:
$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'.
}
这可以做到吗,语法是什么?
【问题讨论】:
-
太棒了,我从来不知道这一点,我花了很长时间才意识到这是问题所在,开关的中断会影响外部 foreach:(来自 php 站点)“请注意,与其他一些语言不同,继续语句适用于 switch,作用类似于 break。如果循环内有 switch,并希望继续到外循环的下一次迭代,请使用 continue 2。"
标签: php control-structure