Migrating Away from the Deprecated each() Function in PHP 7.2
Starting with PHP 7.2, the each() function is officially deprecated. The official documentation warns that relying on this function is highly discouraged and that execution will eventually throw a deprecation notice: "Deprecated: The each() function is deprecated." While PHP often suppresses these messages after the first call to avoid flooding logs, ignoring them is not a sustainable strategy for production applications. The function works by returning an array containing the current key-value pair and advancing the internal pointer of the passed array. This behavior is fragile and tied to specific internal state management that modern PHP engines have moved away from.
To update your codebase, you must identify where each() is used and replace it with standard iteration patterns. The most common pattern involves resetting an array's internal pointer before iterating, followed by a loop that extracts key-value pairs. However, the exact replacement depends heavily on the specific logic surrounding the function call.
Consider a scenario where you are extracting the first element of an array:
$ar = $o->me;
reset($ar);
list($typ, $val) = each($ar);
In this case, each() is used to grab a single entry and implicitly advance the pointer. You can achieve the same result using current() and key():
$ar = $o->me;
$typ = key($ar); // Returns the current key or false if no more elements
$val = current($ar);
// If you need to move to the next element in subsequent calls, use next($ar)
next($ar);
If your logic requires consuming elements sequentially until exhaustion, array_pop() or a standard foreach loop is often cleaner. For instance, if you are processing an array stored in $out:
$out = ['me' => [], 'mytype' => 2, '_php_class' => null];
// Original: $expected = each($out);
$expected = current($out);
$key = key($out);
The most robust migration path for loops that rely on reset() and each() is to switch to a while loop utilizing key(), current(), or simply iterating over the array directly if you do not need random access.
for ($i = 0, $keys = array_keys($broken); reset($broken); $kv = each($broken); $i++) {
// ...
}
// Better approach:
foreach (array_keys($broken) as $k) {
$kv = $broken[$k];
// process
}
One of the trickiest cases involves each() used to iterate until a specific count is reached or the array ends. This pattern often looks like this:
$i = 0;
reset($array);
while( (list($id, $item) = each($array)) || $i < 30 ) {
// code
$i++;
}
Here, the || $i < 30 condition implies that the loop should run a maximum of 30 times even if the array is empty. Replacing this requires careful handling of the termination condition. A standard foreach loop terminates when the array ends, so you cannot easily replicate the "run N times regardless of content" logic without an explicit counter inside the loop:
$count = min(30, count($array));
for ($i = 0; $i < $count && isset($array[$i]); $i++) {
list($id, $item) = [$i, $array[$i]];
// code
}
This approach explicitly manages the index and ensures you stop when either the count limit is hit or the array runs out of elements. Be cautious with list() assignments in this context; they can be verbose. Unpacking directly into variables is usually preferred for readability:
// Instead of: list(, $this->result) = each($this->cache_data);
if (!empty($this->cache_data)) {
$key = key($this->cache_data);
$value = current($this->cache_data);
// Logic to assign to $this->result
}
When refactoring, always verify that you are not relying on the side effect of each() modifying the internal pointer in a way that affects subsequent operations outside the loop scope. Modern PHP provides clearer APIs for array traversal that do not depend on hidden state. If you are working within the Laravel ecosystem, ensure your service providers and legacy controllers have been audited for these calls, as they can cause unexpected warnings during upgrades.
The transition is straightforward but requires attention to detail. By replacing each() with explicit key/value extraction or standard loops, you eliminate deprecation warnings and align your codebase with current PHP standards.
