php - Nested Loop, Break Inner Loops and Continue The Main Loop

Ari picture Ari · Sep 9, 2015 · Viewed 13.8k times · Source

I have the following loop and I want to continue the while loop when the check inside the inner loop has meet the condition. I found a solution here (which is I applied in the following example), but it is for c#.

    $continue = false;
    while($something) {

       foreach($array as $value) {
        if($ok) {
          $continue = true;
           break;
           // continue the while loop
        }

           foreach($value as $val) {
              if($ok) {
              $continue = true;
              break 2;
              // continue the while loop
              }
           }
       }

      if($continue == true) {
          continue;
      }
    }

Is PHP has its own built it way to continue the main loop when the inner loops have been break-ed out?

Answer

Ari picture Ari · Sep 9, 2015

After reading the comment to this question (which was deleted by its author) and did a little research, I found that there is also parameter for continue just like break. We can add number to the continue like so:

while($something) {

   foreach($array as $value) {
    if($ok) {
       continue 2;
       // continue the while loop
    }

       foreach($value as $val) {
          if($ok) {
          continue 3;
          // continue the while loop
          }
       }
   }
}