Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

foreach within foreach...

Status
Not open for further replies.

youradds

Programmer
Joined
Jun 27, 2001
Messages
817
Location
GB
I'm trying to escape out of a main foreach statement. In Perl you can use something like;

Code:
NAME: foreach (@array) {

  if ($var eq "testing") { next NAME; }

}

As I'm sure you can guess, this basically skips the array, and moves onto the next array. Basically, I'm trying to do a similar thing in PHP;

Code:
  $count = 0;
  [red]GOTHROUGH:[/red] foreach ($titles as $_titles) {
  
             if ($_titles == "$title") {

               if ($cats[$count] == "$category") { 
                 $found = 1;
                 [red]continue GOTHROUGH;[/red]
               }
 
             } // end the 'if'

            // if we don't have a match, then lets put it back in...
             if (!$found) { 
               array_push($back_cats,     "$cats[$count]"); 
               array_push($back_titles,   "$_titles"); 
               array_push($back_est1,     "$ourestimate[$count]"); 
               array_push($back_est2,     "$theirestimate[$count]"); 
               array_push($back_est3,     "$actualcost[$count]"); 
               array_push($back_assigned, "$assignedto[$count]"); 
             }

            // increment the counter...
             $count++;

  } // end main 'foreach'

The problem is, that I'm not sure of the syntax. Does anyone know? The current format gives me a parse error.

Any help is much appreciated :)

Cheers

Andy
 
Doesn't break only take you out of one of the foreach's though?

Cheers

Andy
 
Thanks, that worked a treat :)

For future reference, is there such a thing as 'naming' for 'foreach', that will enable you to break out of a specific foreach? I remember seeing something like;

break 1;
break 2;
break 3;

i.e break 1 would break out of the first foreach, break 2 would break out of the current foreach, and another one, break 3 would break out of the current foreach, the one above that, and the one above that. Is this a valid format? I can't seem to find the page again :(

Cheers

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top