PHP foreach: loop arrays with key-value pairs

PHP foreach is the idiomatic way to loop through an array or an object that implements Traversable without manual index tracking. Use foreach ($array as $key => $value) when you need the PHP foreach key and value on each pass. For in-place updates, iterate by reference (&$value) and unset() the loop variable afterward to avoid accidental overwrites.

PHP foreach Example For Key-Value Iteration

Output:

Output will appear here...

Output:

apple: $1.2
bread: $2.5
milk: $0.99

How This Example Works

  1. $prices is an associative array mapping product names to prices.
  2. foreach ($prices as $item => $price) assigns the current key to $item and the current value to $price on each pass — no manual index or count() needed.
  3. Each iteration prints the key-value pair. foreach handles both indexed and associative arrays with the same syntax.

Common Mistakes With PHP foreach

Forgetting unset() after a by-reference loop

Wrong:

foreach ($items as &$item) {
    $item = strtoupper($item);
}
foreach ($items as $item) {} // overwrites the last element

Right:

foreach ($items as &$item) {
    $item = strtoupper($item);
}
unset($item);

After a by-reference foreach, the loop variable still points at the last element. Reusing that variable can cause the last element to be mutated instead of the local loop value.

Expecting by-value loops to modify the array

Wrong:

foreach ($prices as $price) {
    $price *= 1.10; // modifies a copy, not the array
}

Right:

foreach ($prices as &$price) {
    $price *= 1.10;
}
unset($price);

By-value iteration gives each $price a copy of the element. Changes to that copy never reach the original array.

PHP foreach vs for Loop

foreachfor
No index variable needed — cleaner for full traversal.Required when you need index math, step increments, or reverse traversal.
Works on associative arrays and Traversable objects.Works only with numeric indices; associative keys need array_keys().
Less convenient for stepping (e.g., every other item).Natural fit for $i += 2 or counting backward.

Use foreach for standard full-array passes. Switch to for when the loop body depends on numeric position or non-sequential access.

FAQ

How do you get both key and value in a PHP foreach loop?

Use the $key => $value syntax: foreach ($array as $key => $value). This assigns the PHP foreach key ($key) and value ($value) on each iteration for both indexed and associative arrays. If you only need values, use foreach ($array as $value).

Can foreach iterate objects, or only arrays?

foreach works on any value that implements the Traversable interface — including Iterator and IteratorAggregate implementations. For plain objects, it iterates over visible properties based on scope: all properties inside the class, only public properties from outside.

What is the difference between foreach and array_map in PHP?

foreach is a control structure that executes a block for each element, allowing side effects such as echo or an early break. array_map applies a callback and returns a new array. Use array_map when you want a new array and no mutations.