Skip to main content

PHP foreach Loop on Indexed Arrays

When you want to visit every element of an array, or every property of an object, foreach is the natural choice. You don't need a counter, because PHP keeps track of your position for you.

On each pass, the current element's value is placed in the variable you named, here $value. The loop finishes after the last element has been handled.

Loop through an indexed array

Print every item of the indexed $colors array:

Prompt
Write a PHP script that creates an array of colors: "red", "green", "blue", "yellow". Use a foreach loop to echo each color followed by a line break.
Output: PHP script

PHP foreach Loop on Associative Arrays

The elements of an associative array have named keys, and you will often want the key along with the value. To get both, write them in the foreach header as $key => $value.

Loop through an associative array

Each member's name (the key) and age (the value) from $members is printed:

Prompt
Write a PHP script that creates an associative array with members: "Peter"=>"35", "Ben"=>"37", "Joe"=>"43". Use a foreach loop to echo both the key and value, separated by " : ", followed by a line break.
Output: PHP script

PHP foreach Loop on Objects

Objects work with foreach too: the loop goes through their properties one at a time.

Loop through object properties

Here each property name of the $myCar object is printed next to its value:

Prompt
Write a PHP script that defines a Car class with public properties $color and $model, and a constructor that sets them. Create a Car object with color "red" and model "Volvo". Use a foreach loop to echo each property name and value, separated by ": ", followed by a line break.
Output: PHP script

The PHP break Statement

You don't have to go all the way through the array. A break ends the loop before it gets to the last element.

Stop the loop with break

The loop ends when $x holds "blue", so only the colors before it are printed:

Prompt
Write a PHP script that creates an array of colors: "red", "green", "blue", "yellow". Use a foreach loop. If the current color is "blue", use break to stop the loop. Otherwise, echo the color followed by a line break.
Output: PHP script

The PHP continue Statement

Use continue to pass over an element without ending the loop. The rest of that iteration is dropped and the next element takes its turn.

Skip iteration with continue

When $x holds "blue", the echo is skipped, so the other three colors are printed:

Prompt
Write a PHP script that creates an array of colors: "red", "green", "blue", "yellow". Use a foreach loop. If the current color is "blue", use continue to skip it. Otherwise, echo the color followed by a line break.
Output: PHP script

PHP foreach Loop by Reference

Normally the loop variable holds a copy of each item, so assigning a new value to it leaves the original array untouched.

To change the array itself, put the & character in front of the variable in the foreach declaration. The variable then becomes a reference to the array item, and whatever you assign to it ends up in the original array.

Foreach without reference

The loop sets $x to "pink" when it finds "blue", yet var_dump() shows that $colors has not changed:

Prompt
Write a PHP script that creates an array of colors: "red", "green", "blue", "yellow". Use a foreach loop without reference. If the current color is "blue", change the loop variable to "pink". After the loop, use var_dump to show the original array is unchanged.
Output: PHP script

Foreach with reference

With &$x, the same assignment does reach the array, and "blue" is replaced by "pink":

Prompt
Write a PHP script that creates an array of colors: "red", "green", "blue", "yellow". Use a foreach loop with the & reference operator. If the current color is "blue", change the loop variable to "pink". After the loop, use var_dump to show the original array was modified.
Output: PHP script

Alternative Syntax

There is a second way to write a foreach loop: replace the braces with a colon and close the loop with endforeach.

Foreach with endforeach

The indexed $colors array again, printed using the endforeach form:

Prompt
Write a PHP script that creates an array of colors: "red", "green", "blue", "yellow". Use the alternative foreach syntax with endforeach to echo each color followed by a line break.
Output: PHP script