Php Exit Foreach

While handling PHP Arrays foreach function is required to display the elements of an array. Once the foreach function is called the array pointer will reset to the first element of the array. So we need not call the reset again( Reset rewinds array’s internal pointer to the first element. Here is an example of the foreach. The break statement is used to ends the execution of the current loop or switch case statement in PHP. But when working with nested loops and wants to exit out from all or some of the outer loops. For this, you need to pass numeric value following with break statement.

On 9 srp, 12:06, Erwin Moller
<[email protected] yourself.comwrote:
Ivan S schreef:
I'm using next snippet:
$somearray = array(...);
foreach($somearray as $item1) {
* * * foreach($item1 as $item2) {
* * * * * * // ... do something ...
* * *}
...and I'm getting next error:
Invalid argument supplied for foreach()
on second foreach.

Hi Ivan,
Nothing wrong with your code.
The two foreach constructs should be no problem.
I expect that the original array isn't containing what you expect it to
Since you use a foreach on every value in the $somearray, this array
should contain of arrays itself.
Test that like *this:
echo '<pre>';
echo '</pre>';
Erwin Moller
This works on PHP 5, but on PHP 4 I get that error (in Apache logs),
but site seems to work ok.
Is there any way to avoid that error?

Hi Erwin. :)
Your advice helped, some items in $somearray weren't arrays it self,
that was reason why i got errors reported.
Thanks for your help.

Just a few days back, one of my friend asked me how to break a foreach loop in PHP after a certain number of looping. Well, foreach loop is basically the for loop for arrays. It loops until it reaches the end of the array and keeps on executing the block for each of the array content. So, it is named as foreach Loop. This is the basic structure of a foreach loop in PHP:

Obviously we already have set $my_array as an array with definite number of elements. The loop continues to execute on each of the array element until it reaches the end of the array. For example, the following is a code to print all the elements of the an Array…

Now the question is how to break the foreach loop before the end of the Array… Well, the solution is actually pretty simple! We just have to use an if statement to break the foreach loop. Here is how we are going to do this…


Here is the complete explanation…

Php Break Foreach And Return

Php Exit Foreach

How to break the foreach Loop in PHP

Basically we are going to use the following algorithm:

FacebookTwitterLinkedInIn this lesson, you will learn how to use the foreach loop in PHP. The foreach constructs is an easy way to traverse all elements of an array. In this article Introduction to foreach loop in PHPExample of foreach loop in PHPForeach loop for an associative arrayExample of foreach loop with associative arrayExample of foreach. The behavior of foreach statement in PHP for some edge cases is not exactly defined. Actually, it is implementation driven and quite weird. Most these edge cases are related to manipulation with internal pointer and modification of elements of array, iterated by foreach.

  • First we set a variable $count to 0 [Integer Type]
  • Now on each loop of the foreach we increase the value of $count by 1.
  • Just after the increment, inside the foreach loop we check if the value of $count is equal to the desired value of looping. If the value is equal then we just break the loop using break;

Here is the code:

Obviously you can use PHP’s post or get method to set the $max_loop and loop the array for a user defined time, for production purposes! Just check the Source code of my Online Demo to see how things are set…

Php Break Foreach And Continue

Here are the links:

Php Exit Foreach Loop

Well it is as simple as that! I hope you have understand the process… Do give your feedback! And if you face any problem, feel free to ask!