reset
Returns the first element of an array.
Syntax
reset(array|object &$array): mixedParameters
| Parameter | Type | Description |
|---|---|---|
| $array | array|object | An array held in a variable. |
Return value
mixedThe first element, or `false` for an empty array.
Examples
The first element after filtering:
<?php
$big = array_filter([5, 12, 8, 20], fn ($n) => $n > 10);
echo reset($big);Output:
12Notes
After array_filter() index 0 may be missing, while reset() returns the first element whatever the keys.
See also
- end — Returns the last element of an array.
- array_key_first — Returns the first key of an array.
- array_shift — Removes the first element of an array and returns it.
Related recipes
In the tutorial
- Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.