Backend Development 2 min read

PHP each() Function: Returns Current Key/Value Pair and Advances Array Pointer

The article explains PHP's each() function, describing how it returns the current key/value pair from an array while moving the internal pointer forward, details its parameters and return values, notes behavior when the pointer reaches the end, and provides a complete code example with output.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP each() Function: Returns Current Key/Value Pair and Advances Array Pointer

The each() function in PHP returns the current key/value pair of an array and moves the internal array pointer one step forward.

After calling each() , the pointer points to the next element; if the end of the array is reached, the pointer stays on the last element and a subsequent call returns FALSE . To iterate again you must reset the pointer with reset() .

Parameter: array &$array – the array to be traversed.

Return value: An array containing four elements: the key (indices 0 and "key") and the value (indices 1 and "value"). If the internal pointer has passed the last element, each() returns FALSE .

Example:

<?php
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each($foo);
print_r($bar);
?>

Output:

Array
(
    [1] => bob
    [value] => bob
    [0] => 0
    [key] => 0
)
phparrayfunctionExamplepointereach
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.