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:
Output: PHP script
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
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:
Output: PHP script
<?php
$members = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach ($members as $key => $value) {
echo "$key : $value <br>";
}
?>
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:
Output: PHP script
<?php
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
}
$myCar = new Car("red", "Volvo");
foreach ($myCar as $x => $y) {
echo "$x: $y <br>";
}
?>
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:
Output: PHP script
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
if ($x == "blue") break;
echo "$x <br>";
}
?>
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:
Output: PHP script
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
if ($x == "blue") continue;
echo "$x <br>";
}
?>
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:
Output: PHP script
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) {
if ($x == "blue") $x = "pink";
}
var_dump($colors);
?>
Foreach with reference
With &$x, the same assignment does reach the array, and "blue" is replaced by "pink":
Output: PHP script
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as &$x) {
if ($x == "blue") $x = "pink";
}
var_dump($colors);
?>
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:
Output: PHP script
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $x) :
echo "$x <br>";
endforeach;
?>