each()
PHP each() returned the current key-value pair and advanced the array pointer. Deprecated in 7.2, removed in 8.0. Learn its behavior and foreach replacements.
Introduction
The each() function was historically used to step through an array one element at a time, returning the current key-value pair and advancing the array's internal pointer. It powered a once-common while loop idiom for iterating arrays before foreach became the standard.
Important:
each()was deprecated in PHP 7.2 and completely removed in PHP 8.0. Calling it in PHP 8+ throws a fatalError. This page documents its legacy behavior so you can read and migrate old code — but you should never write new code with it. Jump to Modern alternatives for what to use instead.
This page covers what each() did, its syntax and return shape, the internal-pointer behavior that made it tricky, and how to rewrite each usage with foreach.
Syntax
The syntax for the each() function is as follows:
Syntax of Each() function in PHP
each(array $array): array|falseThe each() function takes an array as its parameter and returns an array or false on failure.
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array (by reference) | The array to read from and advance. Works with both indexed and associative arrays. |
The array is passed by reference because each() mutates it — specifically, it moves the array's internal pointer forward on every call.
Return value
On each call, each() returns an array of four elements, with both numeric and string keys pointing at the same data:
| Index | Holds |
|---|---|
0 | the current key |
key | the same value as 0 |
1 | the current value |
value | the same value as 1 |
When the pointer has moved past the last element, each() returns false — which is what stops the classic while loop.
Usage
The classic idiom paired each() with a while loop. Each iteration grabbed one pair and advanced the pointer; when there were no pairs left, each() returned false and the loop ended.
Legacy each() loop (PHP 7.x and earlier)
<?php
// Legacy PHP 7.x example. This will throw a fatal Error in PHP 8.0+.
$array = ["one" => 1, "two" => 2, "three" => 3];
while ($element = each($array)) {
echo $element['key'] . ' => ' . $element['value'] . "\n";
}
// Output:
// one => 1
// two => 2
// three => 3We define an associative array, then call each() repeatedly inside a while. Each call returns the current pair (accessible as $element['key'] / $element['value']) and advances the pointer, until each() finally returns false.
The internal-pointer gotcha
Because each() consumes the internal pointer, an array is only fully traversable once. To loop a second time you had to call reset() first to rewind the pointer to the start. Forgetting this was a frequent source of "my loop does nothing" bugs — and is one reason each() was eventually removed in favor of foreach, which uses its own iterator and never disturbs the array.
Modern PHP 8+ equivalent
The whole while (each()) pattern collapses into a single, clearer foreach:
<?php
$array = ["one" => 1, "two" => 2, "three" => 3];
foreach ($array as $key => $value) {
echo $key . ' => ' . $value . "\n";
}
// Output:
// one => 1
// two => 2
// three => 3foreach is shorter, doesn't touch the internal pointer (so the array stays reusable), and is the only one of the two that runs on PHP 8+.
each() vs. similar array functions
each() is often grouped with other iteration tools, but each does something distinct:
| Function | What it does | Returns a key/value pair? | Touches internal pointer? |
|---|---|---|---|
foreach | Language construct to loop every element | Yes (as $key => $value) | No |
array_walk() | Applies a callback to each element, in place | No | No |
array_map() | Builds a new array from a callback's results | No | No |
current() / next() / key() | Read the element at the pointer / advance it / read its key | Partially (separately) | Yes (next()) |
If you need the manual pointer control that each() once offered, combine current(), key(), next(), and reset() — those functions are still available in PHP 8+.
Legacy status & modern alternatives
The each() function is deprecated (PHP 7.2) and removed (PHP 8.0). For all new code:
- Iterating an array? Use
foreach. - Transforming values into a new array? Use
array_map(). - Modifying elements in place? Use
array_walk(). - Need manual stepping? Use
current()+next()+reset().
Conclusion
each() was once the go-to way to walk an array and grab key-value pairs, but its reliance on the internal pointer made it error-prone, and PHP 8.0 removed it entirely. Treat it as read-only history: recognize it in legacy code, then replace it with foreach — which is safer, faster, and the modern standard.