PHP Reference ยท Array Functions
PHP current(), next(), reset() and end() functions
Reference for PHP's internal array pointer functions current(), key(), next(), prev(), reset() and end(): how they move, what they return and common pitfalls.
Every PHP array has a hidden internal pointer that marks one of its elements. A small family of functions reads and moves it: current() returns the element under the pointer, key() returns its key, next() and prev() move it, reset() moves it to the first element and end() moves it to the last.
Today the pointer is mostly used for two tricks: getting the first or last value of an array in one call (reset($a), end($a)), and stepping through an array manually. For ordinary loops, foreach is simpler and safer.
<?php $a = ["x" => 1, "y" => 2, "z" => 3]; echo reset($a); // 1 echo end($a); // 3
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
current(array|object $array): mixed key(array|object $array): int|string|null next(array|object &$array): mixed prev(array|object &$array): mixed reset(array|object &$array): mixed end(array|object &$array): mixed
| Name | Description |
|---|---|
$array |
The array. current() and key() only read; next(), prev(), reset() and end() take it by reference because they move the pointer. |
Return value:
current(),next(),prev(),reset()andend()return the element's value at the new position, orfalseif there is no element (empty array, or the pointer is past either end).key()returns the key at the pointer, ornullwhen the pointer is beyond the array.
Usage notes
falseis ambiguous. An array that contains the valuefalsegives the same result as running off the end. Test the end of the array withkey($a) !== null, not with the returned value.- Reference parameters.
reset(...)andend(...)need a variable. Passing the result of a function call, as inend(explode(",", $s)), raises a notice, "Only variables should be passed by reference". Assign to a variable first. - Prefer newer helpers for the ends.
array_key_first()andarray_key_last()(PHP 7.3) return keys without touching the pointer, and$a[array_key_last($a)]reads the last value. PHP 8.5 addsarray_first()andarray_last(), which return the values directly. - The pointer belongs to the array copy. Assigning an array copies the pointer position along with it, and functions such as
foreachno longer use or change the pointer (this changed in PHP 7). - Objects: passing an object is deprecated since PHP 8.1. Use an iterator or
get_object_vars()instead. - Sorting functions reset the pointer to the first element; do not rely on the position after modifying an array.
each()was removed in PHP 8.0. Usekey()pluscurrent().