Article

Replacing each()

Remove each() safely: lifecycle, modern replacements, and migration examples for legacy PHP apps.

each() returned the current key/value pair from an array and advanced its internal pointer. Classic patterns combined each() with list() inside while loops.

Lifecycle (php.net)

  • Deprecated: PHP 7.2.0
  • Removed: PHP 8.0.0

Modern replacement

Use foreach, or ArrayIterator when you need an explicit iterator object.

Migration example

# LEGACY — each()
reset($items);
while (list($key, $value) = each($items)) {
    handle($key, $value);
}

# MODERN
foreach ($items as $key => $value) {
    handle($key, $value);
}

Scan before cutover

Search the tree for each() and paste samples into the PHP Version Compatibility Checker and PHP Deprecated Checker.

Related tools

Related reading