ZetCode

PHP foreach

last modified January 10, 2023

PHP foreach tutorial shows how to loop over array elements and object properties in PHP with foreach statement.

$ php -v
php -v
PHP 8.1.2 (cli) (built: Aug  8 2022 07:28:23) (NTS)
...

We use PHP version 8.1.2.

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.

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

This is the output.

PHP foreach example II

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

This is the output.

PHP foreach alternative syntax

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.

PHP foreach multidimensional array

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 

This is the output.

PHP foreach modify array elements

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.

PHP foreach object properties

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

This is the output.

In this article we have presented the PHP foreach statement.

Author

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all PHP tutorials.