PHP foreach tutorial shows how to loop over array elements and object properties in PHP with foreach statement.
last modified February 16, 2025
PHP foreach tutorial shows how to loop over array elements and object properties in PHP with foreach statement.
The foreach statement simplifies traversing over collections of data. The foreach statement goes through the array elements or object properties one by one and the current value is copied to a variable defined in the construct.
The following example loops over array elements.
planets.php
<?php
$planets = [ “Mercury”, “Venus”, “Earth”, “Mars”, “Jupiter”, “Saturn”, “Uranus”, “Neptune” ];
foreach ($planets as $item) {
echo "$item ";
}
echo “\n”;
We have an array of planets. With the foreach statement we go through elements and print them one by one.
$ php planets.php Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune
The next example loops over an array/dictionary.
dictionary.php
<?php
$benelux = [ ‘be’ => ‘Belgium’, ’lu’ => ‘Luxembourgh’, ’nl’ => ‘Netherlands’ ];
foreach ($benelux as $key => $value) {
echo "$key is $value\n";
}
The example prints the key/value pairs of the array.
$ php dictionary.php be is Belgium lu is Luxembourgh nl is Netherlands
PHP supports an alternative syntax with foreach and endforeach;.
altsyn.php
<?php
$planets = [ “Mercury”, “Venus”, “Earth”, “Mars”, “Jupiter”, “Saturn”, “Uranus”, “Neptune” ];
foreach ($planets as $planet):
echo "$planet ";
endforeach;
echo “\n”;
In the example, we loop over the array using the alternative syntax.
We can use multiple foreach statements to loop over multidimensional arrays.
multidim.php
<?php
$vals = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ];
foreach ($vals as $nested) {
foreach ($nested as $val) {
echo $val . ' ';
}
echo "\n";
}
In the example, we use two foreach statements to go over a two-dimensional array of integers.
$ php multidim.php 1 2 3 4 5 6 7 8 9
By using the ampersand operator (&), the foreach statement works with a reference to the array element.
modify.php
<?php
$vals = [1, 2, 3, 4, 5];
foreach ($vals as &$val) {
$val *= 2;
}
print_r($vals);
In the example, we go through the array of integers and multiply each element by two.
$ php modify_array.php Array ( [0] => 2 [1] => 4 [2] => 6 [3] => 8 [4] => 10 )
The array has been modified.
The following example iterates over object properties.
object.php
<?php
class User { public $name; public $occupation;
public function __construct($name, $occupation) {
$this->name = $name;
$this->occupation = $occupation;
}
}
$user = new User(‘John Doe’, ‘gardener’);
foreach ($user as $propName => $propValue) { echo “$propName: $propValue\n”; }
The user object has two properties: $name and $occupation. We loop over those properties with foreach.
$ php object.php name: John Doe occupation: gardener
In this article we have presented the PHP foreach statement.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP tutorials.