63

Is there a way to continue on external foreach in case that the internal foreach meet some statement ?

In example

foreach($c as $v)
{
    foreach($v as $j)
    {
        if($j = 1)
        {
            continue; // But not the internal foreach. the external;
        }
    }
}
KodeFor.Me
  • 13,069
  • 27
  • 98
  • 166
  • 1
    looks like you can...you have to specify how many levels it should continue..in your case continue 2; more info here http://php.net/manual/en/control-structures.continue.php – Catalin Oct 20 '11 at 10:44
  • if you explain the real case with the data and cause, you will get more appropriate answer. -1 so far, for the extreme conciseness and vagueness. – Your Common Sense Oct 20 '11 at 10:45
  • -1 as well because there has not been done much research before asking the question. Not even a look into the manual. – hakre Oct 20 '11 at 10:59
  • 1
    Regardless of SO standards, this is the top result on google. Therefore, I offer the following... This may be a code smell. In some cases wrapping the inner loop in another function/method may be a good idea. – musicin3d Feb 13 '20 at 19:26

7 Answers7

136

Try this, should work:

continue 2;

From the PHP Manual:

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

Adi Prasetyo
  • 1,006
  • 1
  • 15
  • 41
16

Try this: 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. 
matino
  • 17,199
  • 8
  • 49
  • 58
9

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.

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.

foreach($c as $v)
{
    foreach($v as $j)
    {
        if($j = 1)
        {
            continue 2;
        }
    }
    // This code will not be reached.
}
Marcus
  • 12,296
  • 5
  • 48
  • 66
5

This will continue to levels above (so the outer foreach)

 continue 2
JNDPNT
  • 7,445
  • 2
  • 34
  • 40
5
<?php
foreach($c as $v)
{
    foreach($v as $j)
    {
        if($j = 1)
        {
            continue 2; // note the number 2
        }
    }
}
?>

RTM

daiscog
  • 11,441
  • 6
  • 50
  • 62
  • Every answer with a reference to `continue 2` has been downvoted once by someone, for some reason... – daiscog Oct 20 '11 at 10:49
3

Try break instead of continue.

You can follow break with an integer, giving the number of loops to break out of.

TRiG
  • 10,148
  • 7
  • 57
  • 107
0

you have to use break instead of continue, if I get you right

Here I wrote an explanation on the matter: What is meant by a number after "break" or "continue" in PHP?

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345