Statements: Foreach

The foreach statement iterates over the set of elements in a given collection, starting at the beginning, executing a single statement each iteration. On each iteration, the value of the current element is assigned to the corresponding variable, as specified. The loop body is executed zero or more times. For example:

$colors = vec["red", "white", "blue"];
foreach ($colors as $color) {
  ...
}

Here, we iterate over a collection of three strings in a vec of string. Inside the loop body, $color takes on the value of the current string.

See compound statements for a discussion about the use of braces around the statement body.

As each array element has an index as well as a value, we can access both. For example:

$colors = vec["red", "white", "blue"];
foreach ($colors as $key => $color) {
  ...
}

The as clause gives us access to the array key.

We can cause each element's value to be ignored, using $_, as follows:

  $a = dict['a' => 10, 'f' => 30];
  foreach ($a as $key => $_) { // 10 and 30 are ignored
    ...
  }