有没有办法在内部foreach遇到某些语句的情况下继续外部foreach?
示例中
foreach($c as $v)
{
foreach($v as $j)
{
if($j = 1)
{
continue; // But not the internal foreach. the external;
}
}
}发布于 2011-10-20 18:40:07
试试这个,应该行得通:
continue 2;来自PHP手册:
Continue接受一个可选的数值参数,该参数告诉它应该跳到多少层闭合循环的末尾。
示例(第二个)中的here准确地描述了您需要的代码
发布于 2011-10-20 18:41:13
试试这个:根据手册执行continue 2;:
continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of. 发布于 2011-10-20 18:45:03
对于这种情况,有两种可用的解决方案,使用break或continue 2。请注意,当使用break中断内部循环时,内部循环之后的任何代码仍将被执行。
foreach($c as $v)
{
foreach($v as $j)
{
if($j = 1)
{
break;
}
}
echo "This line will be printed";
}另一种解决方案是使用continue,然后使用多少个级别继续。
foreach($c as $v)
{
foreach($v as $j)
{
if($j = 1)
{
continue 2;
}
}
// This code will not be reached.
}https://stackoverflow.com/questions/7834691
复制相似问题