PHP foreach
last modified October 22, 2026
PHP foreach tutorial shows how to loop over array elements and object properties in PHP with foreach statement.
PHP 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.
PHP foreach example
The following example loops over array elements.
<?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
PHP foreach example II
The next example loops over an array/dictionary.
<?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 foreach alternative syntax
PHP supports an alternative syntax with foreach and endforeach;.
<?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.
$ php altsyn.php Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune
PHP foreach multidimensional array
We can use multiple foreach statements to loop over multidimensional
arrays.
<?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
PHP foreach modify array elements
By using the ampersand operator (&), the foreach statement works
with a reference to the array element.
<?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.
PHP foreach object properties
The following example iterates over object properties.
<?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
PHP foreach list destructuring
Each element of an array of arrays can be unpacked into variables directly in the foreach header.
<?php
$users = [
['id' => 1, 'name' => 'John', 'role' => 'admin'],
['id' => 2, 'name' => 'Jane', 'role' => 'user'],
];
foreach ($users as ['id' => $id, 'name' => $name]) {
echo "$id: $name\n";
}
$points = [[1, 2], [3, 4]];
foreach ($points as [$x, $y]) {
echo "x=$x, y=$y\n";
}
The first loop picks two keys from each associative array; the second one unpacks positional elements. Keys that are not listed are ignored.
$ php destructuring.php 1: John 2: Jane x=1, y=2 x=3, y=4
PHP foreach reference pitfall
When we iterate by reference, the loop variable stays a reference to the last element after the loop. Reusing it causes a classic bug.
<?php
$nums = [1, 2, 3];
foreach ($nums as &$n) {
$n *= 2;
}
foreach ($nums as $n) {
// $n is still a reference to $nums[2]
}
print_r($nums);
unset($n);
After the first loop, $n still references the last element. The second loop assigns each value to $n and therefore overwrites the last element, so the array ends with [2, 4, 4] instead of [2, 4, 6]. The fix is to call unset($n) right after a by-reference loop.
$ php reference_pitfall.php
Array
(
[0] => 2
[1] => 4
[2] => 4
)
Source
In this article we have presented the PHP foreach statement.
Author
List all PHP tutorials.