How can I break an outer loop with PHP?PHP: Breaking the nested loopPHP break from 2 loopsPHP Break in Switch Not Working As ExpectedIn Php 'foreach' skip the current iteration block and also rest of the loopHow can I find the array key from second array where the inner keys and values match first array?Break loop from inside loop phpHow can I prevent SQL injection in PHP?Deleting an element from an array in PHPHow do I get PHP errors to display?Loop through an array in JavaScriptHow do you parse and process HTML/XML in PHP?Reference — What does this symbol mean in PHP?“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHPHow do I check if a string contains a specific word?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?
Why did modems have speakers?
Has Peter Parker ever eaten bugs?
What kind of curve (or model) should I fit to my percentage data?
How does mathematics work?
Book in which the "mountain" in the distance was a hole in the flat world
Why are Oscar, India, and X-Ray (O, I, and X) not used as taxiway identifiers?
Would using carbon dioxide as fuel work to reduce the greenhouse effect?
What gave NASA the confidence for a translunar injection in Apollo 8?
What is "It is x o'clock" in Japanese with subject
What is the best word describing the nature of expiring in a short amount of time, connoting "losing public attention"?
How can Kazakhstan perform MITM attacks on all HTTPS traffic?
Is it ethical to tell my teaching assistant that I like him?
Where is this photo of a group of hikers taken? Is it really in the Ural?
Was US film used in Luna 3?
How can I indicate that what I'm saying is not sarcastic online?
Why is DC so, so, so Democratic?
How did pilots avoid thunderstorms and related weather before “reliable” airborne weather radar was introduced on airliners?
Impact of throwing away fruit waste on a peak > 3200 m above a glacier
Do I care if the housing market has gone up or down, if I'm moving from one house to another?
Extrapolation v. Interpolation
Chemistry Riddle
Short story where a flexible reality hardens to an unchanging one
How am I supposed to put out fires?
Why did NASA use Imperial units?
How can I break an outer loop with PHP?
PHP: Breaking the nested loopPHP break from 2 loopsPHP Break in Switch Not Working As ExpectedIn Php 'foreach' skip the current iteration block and also rest of the loopHow can I find the array key from second array where the inner keys and values match first array?Break loop from inside loop phpHow can I prevent SQL injection in PHP?Deleting an element from an array in PHPHow do I get PHP errors to display?Loop through an array in JavaScriptHow do you parse and process HTML/XML in PHP?Reference — What does this symbol mean in PHP?“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHPHow do I check if a string contains a specific word?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am looking to break an outer for/foreach loop in PHP.
This can be done in ActionScript like so:
top : for each(var i:MovieClip in movieClipArray)
for each(var j:String in nameArray)
if(i.name == j) break top;
What's the PHP equivalent?
php for-loop break
add a comment |
I am looking to break an outer for/foreach loop in PHP.
This can be done in ActionScript like so:
top : for each(var i:MovieClip in movieClipArray)
for each(var j:String in nameArray)
if(i.name == j) break top;
What's the PHP equivalent?
php for-loop break
6
Always have a look at the documentation first: php.net/manual/en/control-structures.break.php
– Felix Kling
May 4 '11 at 8:14
I've posted 2 solutions, one with labels, as in action script and other that just usesbreak
– Edgar Villegas Alvarado
May 4 '11 at 8:22
add a comment |
I am looking to break an outer for/foreach loop in PHP.
This can be done in ActionScript like so:
top : for each(var i:MovieClip in movieClipArray)
for each(var j:String in nameArray)
if(i.name == j) break top;
What's the PHP equivalent?
php for-loop break
I am looking to break an outer for/foreach loop in PHP.
This can be done in ActionScript like so:
top : for each(var i:MovieClip in movieClipArray)
for each(var j:String in nameArray)
if(i.name == j) break top;
What's the PHP equivalent?
php for-loop break
php for-loop break
edited Jan 27 '12 at 23:53
Marty
asked May 4 '11 at 8:11
MartyMarty
31.6k18 gold badges77 silver badges148 bronze badges
31.6k18 gold badges77 silver badges148 bronze badges
6
Always have a look at the documentation first: php.net/manual/en/control-structures.break.php
– Felix Kling
May 4 '11 at 8:14
I've posted 2 solutions, one with labels, as in action script and other that just usesbreak
– Edgar Villegas Alvarado
May 4 '11 at 8:22
add a comment |
6
Always have a look at the documentation first: php.net/manual/en/control-structures.break.php
– Felix Kling
May 4 '11 at 8:14
I've posted 2 solutions, one with labels, as in action script and other that just usesbreak
– Edgar Villegas Alvarado
May 4 '11 at 8:22
6
6
Always have a look at the documentation first: php.net/manual/en/control-structures.break.php
– Felix Kling
May 4 '11 at 8:14
Always have a look at the documentation first: php.net/manual/en/control-structures.break.php
– Felix Kling
May 4 '11 at 8:14
I've posted 2 solutions, one with labels, as in action script and other that just uses
break
– Edgar Villegas Alvarado
May 4 '11 at 8:22
I've posted 2 solutions, one with labels, as in action script and other that just uses
break
– Edgar Villegas Alvarado
May 4 '11 at 8:22
add a comment |
6 Answers
6
active
oldest
votes
In the case of 2 nested loops:
break 2;
http://php.net/manual/en/control-structures.break.php
15
that goes forcontinue 2
,just for more info =)
– Arash Moosapour
Jun 27 '17 at 14:00
add a comment |
PHP Manual says
break accepts an optional numeric
argument which tells it how many
nested enclosing structures are to be
broken out of.
break 2;
add a comment |
You can using just a break-n statement:
foreach(...)
foreach(...)
if(i.name == j) break 2; //Breaks 2 levels, so breaks outermost foreach
If you're in php >= 5.3, you can use labels and goto
s, similar as in action script:
foreach(...)
foreach(...)
if(i.name == j) goto top;
top :
But goto must be used carefully. Goto is evil (considered bad practice)
1
There are quite valid uses for goto. Even eval() is not evil (and it is). It has even less uses than goto.
– lucian303
May 31 '13 at 19:14
1
Yes. That's why I said "goto must be used carefully"
– Edgar Villegas Alvarado
Aug 29 '13 at 8:05
In that note I believe this page is relevant: php.net/manual/en/control-structures.goto.php
– Ruggi
May 19 '16 at 14:14
add a comment |
You can use break 2;
to break out of two loops at the same time. It's not quite the same as your example with the "named" loops, but it will do the trick.
add a comment |
$i = new MovieClip();
foreach($movieClipArray as $i)
$nameArray = array();
foreach($nameArray as $n) if($i->name==$n) break 2;
Nice try at converting the entire supplied code to PHP, though the break statement doesn't do what I need it to do (it only ends the inner loop).
– Marty
May 4 '11 at 8:18
refresh you page :) I replaced with break 2 ... which according to PHP Manual: "break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. "
– Jordan Arseno
May 4 '11 at 8:22
add a comment |
Use goto?
for($i=0,$j=50; $i<100; $i++)
while($j--)
if($j==17)
goto end;
echo "i = $i";
end:
echo 'j hit 17';
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5880442%2fhow-can-i-break-an-outer-loop-with-php%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
In the case of 2 nested loops:
break 2;
http://php.net/manual/en/control-structures.break.php
15
that goes forcontinue 2
,just for more info =)
– Arash Moosapour
Jun 27 '17 at 14:00
add a comment |
In the case of 2 nested loops:
break 2;
http://php.net/manual/en/control-structures.break.php
15
that goes forcontinue 2
,just for more info =)
– Arash Moosapour
Jun 27 '17 at 14:00
add a comment |
In the case of 2 nested loops:
break 2;
http://php.net/manual/en/control-structures.break.php
In the case of 2 nested loops:
break 2;
http://php.net/manual/en/control-structures.break.php
answered May 4 '11 at 8:14
lucian303lucian303
2,8221 gold badge13 silver badges10 bronze badges
2,8221 gold badge13 silver badges10 bronze badges
15
that goes forcontinue 2
,just for more info =)
– Arash Moosapour
Jun 27 '17 at 14:00
add a comment |
15
that goes forcontinue 2
,just for more info =)
– Arash Moosapour
Jun 27 '17 at 14:00
15
15
that goes for
continue 2
,just for more info =)– Arash Moosapour
Jun 27 '17 at 14:00
that goes for
continue 2
,just for more info =)– Arash Moosapour
Jun 27 '17 at 14:00
add a comment |
PHP Manual says
break accepts an optional numeric
argument which tells it how many
nested enclosing structures are to be
broken out of.
break 2;
add a comment |
PHP Manual says
break accepts an optional numeric
argument which tells it how many
nested enclosing structures are to be
broken out of.
break 2;
add a comment |
PHP Manual says
break accepts an optional numeric
argument which tells it how many
nested enclosing structures are to be
broken out of.
break 2;
PHP Manual says
break accepts an optional numeric
argument which tells it how many
nested enclosing structures are to be
broken out of.
break 2;
answered May 4 '11 at 8:13
Shakti SinghShakti Singh
67.9k17 gold badges119 silver badges142 bronze badges
67.9k17 gold badges119 silver badges142 bronze badges
add a comment |
add a comment |
You can using just a break-n statement:
foreach(...)
foreach(...)
if(i.name == j) break 2; //Breaks 2 levels, so breaks outermost foreach
If you're in php >= 5.3, you can use labels and goto
s, similar as in action script:
foreach(...)
foreach(...)
if(i.name == j) goto top;
top :
But goto must be used carefully. Goto is evil (considered bad practice)
1
There are quite valid uses for goto. Even eval() is not evil (and it is). It has even less uses than goto.
– lucian303
May 31 '13 at 19:14
1
Yes. That's why I said "goto must be used carefully"
– Edgar Villegas Alvarado
Aug 29 '13 at 8:05
In that note I believe this page is relevant: php.net/manual/en/control-structures.goto.php
– Ruggi
May 19 '16 at 14:14
add a comment |
You can using just a break-n statement:
foreach(...)
foreach(...)
if(i.name == j) break 2; //Breaks 2 levels, so breaks outermost foreach
If you're in php >= 5.3, you can use labels and goto
s, similar as in action script:
foreach(...)
foreach(...)
if(i.name == j) goto top;
top :
But goto must be used carefully. Goto is evil (considered bad practice)
1
There are quite valid uses for goto. Even eval() is not evil (and it is). It has even less uses than goto.
– lucian303
May 31 '13 at 19:14
1
Yes. That's why I said "goto must be used carefully"
– Edgar Villegas Alvarado
Aug 29 '13 at 8:05
In that note I believe this page is relevant: php.net/manual/en/control-structures.goto.php
– Ruggi
May 19 '16 at 14:14
add a comment |
You can using just a break-n statement:
foreach(...)
foreach(...)
if(i.name == j) break 2; //Breaks 2 levels, so breaks outermost foreach
If you're in php >= 5.3, you can use labels and goto
s, similar as in action script:
foreach(...)
foreach(...)
if(i.name == j) goto top;
top :
But goto must be used carefully. Goto is evil (considered bad practice)
You can using just a break-n statement:
foreach(...)
foreach(...)
if(i.name == j) break 2; //Breaks 2 levels, so breaks outermost foreach
If you're in php >= 5.3, you can use labels and goto
s, similar as in action script:
foreach(...)
foreach(...)
if(i.name == j) goto top;
top :
But goto must be used carefully. Goto is evil (considered bad practice)
edited May 12 '14 at 5:33
answered May 4 '11 at 8:17
Edgar Villegas AlvaradoEdgar Villegas Alvarado
16.6k1 gold badge34 silver badges54 bronze badges
16.6k1 gold badge34 silver badges54 bronze badges
1
There are quite valid uses for goto. Even eval() is not evil (and it is). It has even less uses than goto.
– lucian303
May 31 '13 at 19:14
1
Yes. That's why I said "goto must be used carefully"
– Edgar Villegas Alvarado
Aug 29 '13 at 8:05
In that note I believe this page is relevant: php.net/manual/en/control-structures.goto.php
– Ruggi
May 19 '16 at 14:14
add a comment |
1
There are quite valid uses for goto. Even eval() is not evil (and it is). It has even less uses than goto.
– lucian303
May 31 '13 at 19:14
1
Yes. That's why I said "goto must be used carefully"
– Edgar Villegas Alvarado
Aug 29 '13 at 8:05
In that note I believe this page is relevant: php.net/manual/en/control-structures.goto.php
– Ruggi
May 19 '16 at 14:14
1
1
There are quite valid uses for goto. Even eval() is not evil (and it is). It has even less uses than goto.
– lucian303
May 31 '13 at 19:14
There are quite valid uses for goto. Even eval() is not evil (and it is). It has even less uses than goto.
– lucian303
May 31 '13 at 19:14
1
1
Yes. That's why I said "goto must be used carefully"
– Edgar Villegas Alvarado
Aug 29 '13 at 8:05
Yes. That's why I said "goto must be used carefully"
– Edgar Villegas Alvarado
Aug 29 '13 at 8:05
In that note I believe this page is relevant: php.net/manual/en/control-structures.goto.php
– Ruggi
May 19 '16 at 14:14
In that note I believe this page is relevant: php.net/manual/en/control-structures.goto.php
– Ruggi
May 19 '16 at 14:14
add a comment |
You can use break 2;
to break out of two loops at the same time. It's not quite the same as your example with the "named" loops, but it will do the trick.
add a comment |
You can use break 2;
to break out of two loops at the same time. It's not quite the same as your example with the "named" loops, but it will do the trick.
add a comment |
You can use break 2;
to break out of two loops at the same time. It's not quite the same as your example with the "named" loops, but it will do the trick.
You can use break 2;
to break out of two loops at the same time. It's not quite the same as your example with the "named" loops, but it will do the trick.
answered May 4 '11 at 8:13
JonJon
356k61 gold badges630 silver badges725 bronze badges
356k61 gold badges630 silver badges725 bronze badges
add a comment |
add a comment |
$i = new MovieClip();
foreach($movieClipArray as $i)
$nameArray = array();
foreach($nameArray as $n) if($i->name==$n) break 2;
Nice try at converting the entire supplied code to PHP, though the break statement doesn't do what I need it to do (it only ends the inner loop).
– Marty
May 4 '11 at 8:18
refresh you page :) I replaced with break 2 ... which according to PHP Manual: "break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. "
– Jordan Arseno
May 4 '11 at 8:22
add a comment |
$i = new MovieClip();
foreach($movieClipArray as $i)
$nameArray = array();
foreach($nameArray as $n) if($i->name==$n) break 2;
Nice try at converting the entire supplied code to PHP, though the break statement doesn't do what I need it to do (it only ends the inner loop).
– Marty
May 4 '11 at 8:18
refresh you page :) I replaced with break 2 ... which according to PHP Manual: "break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. "
– Jordan Arseno
May 4 '11 at 8:22
add a comment |
$i = new MovieClip();
foreach($movieClipArray as $i)
$nameArray = array();
foreach($nameArray as $n) if($i->name==$n) break 2;
$i = new MovieClip();
foreach($movieClipArray as $i)
$nameArray = array();
foreach($nameArray as $n) if($i->name==$n) break 2;
answered May 4 '11 at 8:17
Jordan ArsenoJordan Arseno
4,8005 gold badges40 silver badges88 bronze badges
4,8005 gold badges40 silver badges88 bronze badges
Nice try at converting the entire supplied code to PHP, though the break statement doesn't do what I need it to do (it only ends the inner loop).
– Marty
May 4 '11 at 8:18
refresh you page :) I replaced with break 2 ... which according to PHP Manual: "break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. "
– Jordan Arseno
May 4 '11 at 8:22
add a comment |
Nice try at converting the entire supplied code to PHP, though the break statement doesn't do what I need it to do (it only ends the inner loop).
– Marty
May 4 '11 at 8:18
refresh you page :) I replaced with break 2 ... which according to PHP Manual: "break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. "
– Jordan Arseno
May 4 '11 at 8:22
Nice try at converting the entire supplied code to PHP, though the break statement doesn't do what I need it to do (it only ends the inner loop).
– Marty
May 4 '11 at 8:18
Nice try at converting the entire supplied code to PHP, though the break statement doesn't do what I need it to do (it only ends the inner loop).
– Marty
May 4 '11 at 8:18
refresh you page :) I replaced with break 2 ... which according to PHP Manual: "break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. "
– Jordan Arseno
May 4 '11 at 8:22
refresh you page :) I replaced with break 2 ... which according to PHP Manual: "break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. "
– Jordan Arseno
May 4 '11 at 8:22
add a comment |
Use goto?
for($i=0,$j=50; $i<100; $i++)
while($j--)
if($j==17)
goto end;
echo "i = $i";
end:
echo 'j hit 17';
add a comment |
Use goto?
for($i=0,$j=50; $i<100; $i++)
while($j--)
if($j==17)
goto end;
echo "i = $i";
end:
echo 'j hit 17';
add a comment |
Use goto?
for($i=0,$j=50; $i<100; $i++)
while($j--)
if($j==17)
goto end;
echo "i = $i";
end:
echo 'j hit 17';
Use goto?
for($i=0,$j=50; $i<100; $i++)
while($j--)
if($j==17)
goto end;
echo "i = $i";
end:
echo 'j hit 17';
answered May 4 '11 at 8:16
Petr AbdulinPetr Abdulin
24.5k6 gold badges49 silver badges83 bronze badges
24.5k6 gold badges49 silver badges83 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5880442%2fhow-can-i-break-an-outer-loop-with-php%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
6
Always have a look at the documentation first: php.net/manual/en/control-structures.break.php
– Felix Kling
May 4 '11 at 8:14
I've posted 2 solutions, one with labels, as in action script and other that just uses
break
– Edgar Villegas Alvarado
May 4 '11 at 8:22