Skip to content
Tutorials Center
My learning

Topics

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
<?php
$a = ["x" => 1, "y" => 2, "z" => 3];
echo reset($a);   // 1
echo end($a);     // 3

Try it

PHP Moving the pointer

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.

PHP First and last values, and a manual loop

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

PHP
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() and end() return the element's value at the new position, or false if there is no element (empty array, or the pointer is past either end).
  • key() returns the key at the pointer, or null when the pointer is beyond the array.

Usage notes

  • false is ambiguous. An array that contains the value false gives the same result as running off the end. Test the end of the array with key($a) !== null, not with the returned value.
  • Reference parameters. reset(...) and end(...) need a variable. Passing the result of a function call, as in end(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() and array_key_last() (PHP 7.3) return keys without touching the pointer, and $a[array_key_last($a)] reads the last value. PHP 8.5 adds array_first() and array_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 foreach no 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. Use key() plus current().