each
Returns the current key/value pair of an array and advances the pointer; removed in PHP 8.0.
each(array|object &$array): array- Added in PHP 4.0
- Deprecated since PHP 7.2.0
- Removed in PHP 8.0.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array|object | The array to read the current element from. Passed by reference because the call advances the array's internal pointer. |
Return value
arrayReturns a four-element array with keys 0 and 'key' holding the element's key and keys 1 and 'value' holding its value, or false when the internal pointer is already past the end of the array.
Errors
On PHP 8.0 and later, calling each() throws Error: Call to undefined function each().
Changelog
- PHP 8.0.0The function was removed from PHP entirely.
- PHP 7.2.0The function was deprecated and every call raised E_DEPRECATED.
each() no longer exists. It was deprecated in PHP 7.2 and removed in PHP 8.0, so calling it today throws Error: Call to undefined function each(). This page exists because a lot of pre-PHP 7 code still uses it and you need to know what it did and what to write instead.
It returned a four-element array describing the element the array's internal pointer was on — keys 0 and key held the key, keys 1 and value held the value — and moved the pointer forward one step. Once the pointer ran off the end it returned false, which is what terminated the classic while (list($k, $v) = each($arr)) loop.
What to use instead
- To walk a whole array:
foreach ($arr as $k => $v). This replaces virtually every real use, and it is faster because it iterates a copy and never touches the internal pointer. - To step one element at a time under your own control:
reset()first, thenkey()andcurrent()to read, andnext()to advance.
It was removed precisely because that internal pointer was a language-level obstacle, and because a stale pointer from an earlier loop silently made the next one start in the middle.
Example
<?php
// Legacy PHP 5 style - this FATALS on PHP 8.
$stock = ['apples' => 3, 'pears' => 0];
reset($stock);
while (list($item, $qty) = each($stock)) {
echo "$item: $qty\n";
}
// The PHP 8 replacement:
foreach ($stock as $item => $qty) {
echo "$item: $qty\n";
}When to use it
- Recognising the legacy while (list($k, $v) = each($arr)) loop when reading old code.
- Migrating a PHP 5 script to PHP 8 by rewriting each() loops as foreach.
- Explaining why a copied snippet from an old tutorial fatals on a modern interpreter.
Frequently asked questions
Why does each() give "Call to undefined function" in PHP 8?
Because each() was removed from the language in PHP 8.0 after being deprecated in 7.2. There is no ini setting or compatibility mode that brings it back; the code has to be rewritten.
What replaces each() in PHP 8?
foreach ($array as $key => $value) replaces it in nearly every case. If you genuinely need manual stepping, use reset(), key(), current() and next() together.
Can I write my own each() polyfill?
You can define a function that reads key()/current() and calls next(), but you should not — it reintroduces the internal-pointer bugs that got each() removed, and every static analyser will flag it. Rewriting the loop as foreach takes less time.
More examples
The foreach replacement
Prints one padded line per item and then "total: 15". This is what every each() loop should become: it handles the zero quantity for pears correctly, whereas an each() loop combined with a truthiness test on the value would have been a classic source of bugs.
<?php
$stock = ['apples' => 3, 'pears' => 0, 'plums' => 12];
foreach ($stock as $item => $qty) {
echo str_pad($item, 8), $qty, "\n";
}
echo "total: ", array_sum($stock), "\n";Stepping manually with key, current and next
Prints 0 => red, 1 => green, 2 => blue. This is the closest literal translation of each(): key() and current() read the element the pointer is on, next() advances it, and key() returning null signals the end. Note the strict !== null check, since key 0 is falsy.
<?php
$colors = ['red', 'green', 'blue'];
reset($colors);
while (($k = key($colors)) !== null) {
echo $k, ' => ', current($colors), "\n";
next($colors);
}Checking before you run legacy code
On PHP 8 this prints bool(false) and the warning message. Wrapping a compatibility check like this around old code is a quick way to audit a legacy file before you upgrade an interpreter.
<?php
var_dump(function_exists('each'));
if (!function_exists('each')) {
echo "each() was removed in PHP 8.0 - rewrite the loop as foreach\n";
}
echo 'running on PHP ', PHP_VERSION, "\n";
Discussion