W3docs

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 fatal Error. 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|false

The each() function takes an array as its parameter and returns an array or false on failure.

Parameters

ParameterTypeDescription
$arrayarray (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:

IndexHolds
0the current key
keythe same value as 0
1the current value
valuethe 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 => 3

We 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 => 3

foreach 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:

FunctionWhat it doesReturns a key/value pair?Touches internal pointer?
foreachLanguage construct to loop every elementYes (as $key => $value)No
array_walk()Applies a callback to each element, in placeNoNo
array_map()Builds a new array from a callback's resultsNoNo
current() / next() / key()Read the element at the pointer / advance it / read its keyPartially (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:

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.

Practice

Practice
Which statements about the each() function in PHP are correct?
Which statements about the each() function in PHP are correct?
Was this page helpful?