PHP,继续;在 foreach(){ foreach(){
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7834691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP, continue; on foreach(){ foreach(){
提问by Merianos Nikos
Is there a way to continue on external foreach in case that the internal foreach meet some statement ?
如果内部 foreach 遇到某些语句,有没有办法继续外部 foreach?
In example
例如
foreach($c as $v)
{
foreach($v as $j)
{
if($j = 1)
{
continue; // But not the internal foreach. the external;
}
}
}
回答by user973254
Try this, should work:
试试这个,应该工作:
continue 2;
From the PHP Manual:
来自 PHP 手册:
Continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of.
Continue 接受一个可选的数字参数,它告诉它应该跳到结束的封闭循环的级别。
herein the examples (2nd exactly) described code you need
在您需要的示例(第二个)中描述的代码中
回答by matino
Try this: continue 2;
According to manual:
试试这个:continue 2;
根据手册:
continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of.
回答by Marcus
There are two solutions available for this situation, either use break
or continue 2
. Note that when using break to break out of the internal loop any code after the inner loop will still be executed.
有两种解决方案可用于这种情况,使用break
或continue 2
。请注意,当使用 break 跳出内部循环时,内部循环之后的任何代码仍将被执行。
foreach($c as $v)
{
foreach($v as $j)
{
if($j = 1)
{
break;
}
}
echo "This line will be printed";
}
The other solution is to use continue
followed with how many levels back to continue from.
另一种解决方案是使用continue
后跟从多少个级别返回继续。
foreach($c as $v)
{
foreach($v as $j)
{
if($j = 1)
{
continue 2;
}
}
// This code will not be reached.
}
回答by JNDPNT
This will continue to levels above (so the outer foreach)
这将继续高于(因此外部 foreach)
continue 2
回答by daiscog
回答by TRiG
回答by Your Common Sense
you have to use break
instead of continue, if I get you right
break
如果我说得对,你必须使用而不是继续
Here I wrote an explanation on the matter: What is meant by a number after "break" or "continue" in PHP?
在这里我写了一个关于这个问题的解释:PHP中“break”或“continue”后面的数字是什么意思?