reset
Rewinds an array's internal pointer to the first element and returns that element's value.
reset(array|object &$array): mixed- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array|object | The array whose internal pointer is rewound. Passed by reference. Passing an object is deprecated as of PHP 8.1. |
Return value
mixedThe value of the first element, or false if the array is empty.
Errors
Nothing is thrown. Passing an object emits an E_DEPRECATED notice as of PHP 8.1.0.
Changelog
- PHP 8.1Calling this function on objects is deprecated. Convert the object with get_mangled_object_vars() first, or use a class implementing Iterator such as ArrayIterator.
- PHP 7.4Instances of SPL classes are treated like empty objects with no properties instead of calling the Iterator method of the same name.
reset does two things at once: it moves the array's internal pointer back to the first element, and it returns that element's value. That double duty is why it shows up in two very different kinds of code.
When to reach for it
The first use is pointer housekeeping — after next, prev or end have walked the pointer somewhere, reset puts it back at the top so the next manual pass starts cleanly. It is the only way to recover a pointer that prev pushed off the front.
The second use, far more common, is reading the first value of an array whose keys you do not know: the first row of a grouped result, the first match from a filtered set, the head of a queue. reset($rows) gets it without hardcoding [0], which may not exist after array_filter.
What trips people up
The false return for an empty array is indistinguishable from a first element that is literally false. Guard with $arr === [] or check key($arr) !== null afterwards.
Because the parameter is by reference, reset(array_filter($x)) is an error — assign to a variable first. If you only want the first value and do not care about the pointer, array_values($arr)[0] or $arr[array_key_first($arr)] reads more clearly and works on expressions.
Example
When to use it
- Grab the first row of a query result whose keys were renumbered or preserved unpredictably.
- Restore the array pointer to the start after end()/prev() have walked it backwards.
- Read the head of a queue array without assuming the index 0 still exists after filtering.
Frequently asked questions
What is the difference between reset and array_key_first?
reset() moves the internal pointer and returns the first value; array_key_first() returns the first key without touching the pointer. Use array_key_first when you want the key, or when you cannot pass a variable by reference.
Why does reset() give an error on a function call?
The parameter is declared by reference, so the argument must be a variable. Assign the function result to a variable first: $r = array_filter($x); reset($r);
How do I get the last element instead?
Call end($array), which moves the pointer to the last element and returns its value, or use $array[array_key_last($array)] to leave the pointer alone.
More examples
First element of a filtered array
array_filter kept the key 'b', so $adults[0] does not exist. reset() returns 45 and leaves key() pointing at 'b'; array_key_first is the equivalent that does not touch the pointer.
<?php
$users = ['a' => 12, 'b' => 45, 'c' => 7];
$adults = array_filter($users, fn($age) => $age >= 18);
$first = reset($adults);
echo 'value: ', $first, "\n";
echo 'key: ', key($adults), "\n";
echo 'modern: ', $adults[array_key_first($adults)], "\n";The false-return ambiguity
Both reset() calls print bool(false), so the return value alone cannot tell an empty array from one starting with false. The === [] check and the key() test both distinguish them correctly.
<?php
$flags = [false, true];
$none = [];
var_dump(reset($flags));
var_dump(reset($none));
var_dump($none === []);
var_dump(key($flags) !== null, key($none) !== null);
Discussion