简体   繁体   English

PHP,继续; on foreach(){foreach(){

[英]PHP, continue; on foreach(){ foreach(){

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;
        }
    }
}

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. 继续接受一个可选的数字参数,该参数告诉它应跳过多少级别的封闭循环到结尾。

here in the examples (2nd exactly) described code you need 这里是您需要的示例(准确的第2个)描述的代码

Try this: continue 2; 试试这个: continue 2; According to manual: 根据手册:

continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of. 

There are two solutions available for this situation, either use break or continue 2 . 这种情况有两种解决方案,可以使用breakcontinue 2 Note that when using break to break out of the internal loop any code after the inner loop will still be executed. 请注意,当使用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.
}

这将继续高于水平(所以外部的foreach)

 continue 2
<?php
foreach($c as $v)
{
    foreach($v as $j)
    {
        if($j = 1)
        {
            continue 2; // note the number 2
        }
    }
}
?>

RTM RTM

Try break instead of continue . 尝试break而不是continue

You can follow break with an integer, giving the number of loops to break out of. 您可以使用整数跟随break ,给出要突破的循环数。

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”之后的数字是什么意思?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM