reset()
When working with arrays in PHP, it is often necessary to reset the array pointer to the beginning. The PHP function array reset() allows us to do just that. In
Every PHP array keeps a hidden internal pointer that marks a "current" element. Functions like current(), key(), next(), and prev() read and move this pointer. After you have walked through an array, the pointer is left at the end (or somewhere in the middle) and the next read returns the wrong element. The reset() function rewinds that pointer back to the very first element so you can start over.
This article explains how the internal pointer works, what reset() returns, where it commonly trips people up, and how it fits with the other array-pointer functions.
What is reset()?
reset() is a built-in PHP function that moves an array's internal pointer to the first element and returns that element's value. It is the counterpart of end(), which moves the pointer to the last element.
Crucially, reset() mutates the array's pointer — that is why its signature takes the array by reference (&$array). You pass a variable, not a literal, and reset() changes the pointer state of that variable in place.
Syntax
reset(array &$array): mixedParameters
| Parameter | Description |
|---|---|
$array | The array whose internal pointer you want to rewind. It is passed by reference, so it must be a variable (you cannot pass an inline array literal). |
Return value
reset() returns the value of the first element of the array, or false if the array is empty.
A subtle trap: because an empty array returns false, you cannot reliably distinguish "empty array" from "first element is literally false" by checking the return value alone. If you need that distinction, check the array with empty() or count() first.
Examples
Let's look at how reset() behaves with different kinds of arrays.
Example 1: Resetting the pointer of a numerical array
Example of resetting the pointer of a numerical array in PHP
In this example, we have an array of colors. We call the reset() function to reset the pointer to the beginning of the array. We then call current() to get the first element, which is 'red'.
Example 2: Resetting the pointer of an associative array
Example of resetting the pointer of an associative array in PHP
In this example, we have an associative array of a person's details. We call the reset() function to reset the pointer to the beginning of the array. We then call key() and current() to get the key-value pair of the first element.
Example 3: Why reset() matters after iterating
This is the most common real-world reason to call reset(). Functions that move the pointer (next(), end(), a manual while loop) leave it parked. Without reset(), the next read picks up where you left off:
<?php
$colors = ['red', 'green', 'blue'];
// Advance the pointer to the end
end($colors); // pointer now at 'blue'
echo current($colors); // outputs 'blue'
echo "\n";
// Rewind it
reset($colors);
echo current($colors); // outputs 'red'reset() returns the first value too, so you can grab it directly: $first = reset($colors); gives 'red'.
reset() vs. foreach
A frequent point of confusion: a foreach loop does not use the array's internal pointer. PHP iterates over an internal copy, so the pointer is untouched and you do not need to call reset() after a foreach:
<?php
$colors = ['red', 'green', 'blue'];
foreach ($colors as $color) {
// ... do something
}
// Pointer was never moved by foreach
echo current($colors); // outputs 'red'You only need reset() when you (or a function you call) advance the pointer with the pointer-based functions. See foreach for how looping really works.
Common gotchas
- Pass a variable, not a literal. Because the argument is by reference,
reset(['a', 'b'])raises an error in modern PHP. Assign the array to a variable first. reset()mutates state. If another part of your code relies on the current pointer position, callingreset()will silently change it. Pass a copy if you must preserve the original pointer.falseis ambiguous. Afalsereturn can mean "empty array" or "first value isfalse". Guard withempty($array)when it matters.
Related functions
| Function | Moves the pointer to | Returns |
|---|---|---|
reset() | First element | First value (or false) |
end() | Last element | Last value (or false) |
next() | Next element | Next value (or false) |
prev() | Previous element | Previous value (or false) |
current() | (no move) | Current value |
key() | (no move) | Current key |
Conclusion
reset() rewinds an array's internal pointer to the first element and returns that element's value. Reach for it whenever a previous operation (next(), end(), or a manual pointer loop) has left the pointer somewhere other than the start and you need to begin reading from the top again. Remember that foreach does not touch the pointer, that the argument must be a variable, and that a false return is ambiguous for empty arrays.