PHP.nl

foreach

foreach

The construct provides an easy way to iterate over s and objects. will issue an error when used with a variable containing a different data type or with an uninitialized variable.

foreach``array``Traversable``foreach can optionally get the of each element: foreach``key

foreach (iterable_expression as $value) {
    statement_list
}

foreach (iterable_expression as $key => $value) {
    statement_list
}

The first form traverses the iterable given by . On each iteration, the value of the current element is assigned to . iterable_expression``$value

The second form will additionally assign the current element's key to the variable on each iteration. $key

Note that does not modify the internal array pointer, which is used by functions such as and . foreach``current``key

It is possible to . customize object iteration

Voorbeeld: Common usages

<?php

/* Example: value only */
$array = [1, 2, 3, 17];

foreach ($array as $value) {
    echo "Current element of \$array: $value.\n";
}

/* Example: key and value */
$array = [
    "one" => 1,
    "two" => 2,
    "three" => 3,
    "seventeen" => 17
];

foreach ($array as $key => $value) {
    echo "Key: $key => Value: $value\n";
}

/* Example: multi-dimensional key-value arrays */
$grid = [];
$grid[0][0] = "a";
$grid[0][1] = "b";
$grid[1][0] = "y";
$grid[1][1] = "z";

foreach ($grid as $y => $row) {
    foreach ($row as $x => $value) {
        echo "Value at position x=$x and y=$y: $value\n";
    }
}

/* Example: dynamic arrays */
foreach (range(1, 5) as $value) {
    echo "$value\n";
}
?>

Opmerking: > does not support the ability to suppress error messages using the . foreach@

Unpacking nested arrays

It is possible to iterate over an array of arrays and unpack the nested array into loop variables by using either

via or by using the language construct as the value.

array destructuring[]``list> Opmerking: > Please note that

 via  is only possible as of PHP 7.1.0
array destructuring`[]`
 In both of the following examples  will be set to
 the first element of the nested array and  will
 contain the second element:
`$a``$b`
<?php
$array = [
    [1, 2],
    [3, 4],
];

foreach ($array as [$a, $b]) {
    echo "A: $a; B: $b\n";
}

foreach ($array as list($a, $b)) {
    echo "A: $a; B: $b\n";
}
?>
A: 1; B: 2
A: 3; B: 4

When providing fewer variables than there are elements in the array, the remaining elements will be ignored. Similarly, elements can be skipped over by using a comma:

<?php
$array = [
  [1, 2, 5],
  [3, 4, 6],
];

foreach ($array as [$a, $b]) {
  // Note that there is no $c here.
  echo "$a $b\n";
}

foreach ($array as [, , $c]) {
  // Skipping over $a and $b
  echo "$c\n";
}
?>
1 2
3 4
5
6

A notice will be generated if there aren't enough array elements to fill the :

`list````php


```php
Notice: Undefined offset: 2 in example.php on line 7
A: 1; B: 2; C:

Notice: Undefined offset: 2 in example.php on line 7
A: 3; B: 4; C:

foreach and references

It is possible to directly modify array elements within a loop by preceding with . In that case the value will be assigned by .

$value``&amp;reference```php




> **Waarschuwing:** > Reference to a  of the last array element
>     remain even after the  loop. It is recommended
>     to destroy these using .
>     Otherwise, the following behavior will occur:
>    `$value``foreach``unset`
> 
> ```php
> <?php
> $arr = [1, 2, 3, 4];
> foreach ($arr as &$value) {
>     $value = $value * 2;
> }
> // $arr is now [2, 4, 6, 8]
> 
> // without an unset($value), $value is still a reference to the last item: $arr[3]
> 
> foreach ($arr as $key => $value) {
>     // $arr[3] will be updated with each value from $arr...
>     echo "{$key} => {$value} ";
>     print_r($arr);
> }
> // ...until ultimately the second-to-last value is copied onto the last value
> ?>
> ```
> 
> ```php
> 0 => 2 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 2 )
> 1 => 4 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 4 )
> 2 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 6 )
> 3 => 6 Array ( [0] => 2, [1] => 4, [2] => 6, [3] => 6 )
> ```

**Voorbeeld: Iterate a constant array's values by reference**

```php
<?php
foreach ([1, 2, 3, 4] as &$value) {
    $value = $value * 2;
}
?>

arrayTraversableiterablelist