PHP each() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, each() was a function that returned the current key and value of an array as a pair and then moved the internal pointer forward. It was deprecated in PHP 7.2 and removed in PHP 8.0.

You should not use each() in new code. Calling it in PHP 8 causes a fatal error because the function no longer exists. This page explains what each() used to do and shows the modern replacement: foreach.

Syntax

Call each() with the values you need. Start with this line.

PHP
// Removed in PHP 8.0 - use foreach instead
foreach ($fruits as $key => $value) { ... }
CallWhat it does
foreach ($arr as $key => $value)The modern way to read each key and value.
key($arr) and current($arr)Read the key and value at the internal pointer.
next($arr)Move the internal pointer forward, like each() did.

REMOVED in PHP 8.0. Old code like while (list($key, $value) = each($array)) must be rewritten as foreach ($array as $key => $value). The examples below run on any modern PHP version.

Library declarations
PHP
each(array|object &$array): array

Parameters

ParameterDescription
arrayRequired. Historical: the array to read from. It was passed by reference because each() moved the internal pointer.

Return Value

Return ValueDescription
arrayHistorically, a four-item array with the keys 0, 1, "key" and "value" holding the key and value at the pointer. It returned false when the pointer was past the end.

PHP Examples

Example 1 — Modern Replacement: foreach

Use foreach to read every key and value. This works in all current PHP versions.

PHP
<?php
$fruits = ["a" => "apple", "b" => "banana"];

foreach ($fruits as $key => $value) {
    echo $key, " => ", $value, PHP_EOL;
}

How It Works

  1. Create an array with two keyed items.
  2. foreach visits each item in order.
  3. $key holds the key and $value holds the value for that pass.
  4. Each pair is printed on its own line.

Example 2 — What each() Used to Return

This example only builds the old result by hand, so it runs on modern PHP. It shows the shape each() returned for the first item.

PHP
<?php
$fruits = ["a" => "apple", "b" => "banana"];

$key = key($fruits);
$value = current($fruits);

// Simulates the array that each() returned in PHP 7 and older
$pair = [1 => $value, "value" => $value, 0 => $key, "key" => $key];

echo json_encode($pair), PHP_EOL;
echo "key: ", $pair["key"], PHP_EOL;
echo "value: ", $pair["value"], PHP_EOL;

How It Works

  1. key() and current() read the key and value at the pointer without moving it.
  2. The old each() packed both into an array under the keys 0, 1, "key" and "value". We rebuild that array by hand.
  3. json_encode() shows the four-entry shape that each() used to return.
  4. The last two lines read the "key" and "value" entries, just as old code did.

Example 3 — Rewrite an each() Loop

The old while (list($k, $v) = each($arr)) loop can be written with the pointer functions or, better, with foreach.

PHP
<?php
$colors = ["red", "green", "blue"];

// Pointer version: the same idea as each(), without each()
while (($index = key($colors)) !== null) {
    echo $index, ": ", current($colors), PHP_EOL;
    next($colors);
}

// Preferred version
foreach ($colors as $index => $color) {
    echo $index, "=", $color, PHP_EOL;
}

How It Works

  1. key() returns null when the pointer is past the end, which stops the first loop.
  2. Inside the loop, current() reads the value and next() moves the pointer forward.
  3. This does exactly what the old each() loop did, using functions that still exist.
  4. The foreach version gives the same information in much less code and is the recommended replacement.

Frequently Asked Questions

No. each() was deprecated in PHP 7.2 and removed in PHP 8.0. Calling it in PHP 8 causes a fatal error.
Use foreach ($array as $key => $value). It is the standard way to loop through an array.
It returned an array with the keys 0, 1, "key" and "value" for the current item, then moved the pointer forward. It returned false at the end of the array.
Replace it with foreach ($arr as $k => $v). If you need manual control, use key(), current() and next().
It was confusing, easy to misuse and made code slower. foreach does the same job more clearly.

Next: end()

end() moves the internal pointer of an array to its last element and returns the value of .

end() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful