PHP 7.3 gave you array_key_first() and array_key_last() for getting the first and last keys of an array, but there was still no simple way to get the first and last values. This RFC from Niels Dossche adds array_first() and array_last() to fill that gap.

Why it was needed

Array keys don't have to start at 0, or even be integers, so $array[0] won't always work. The old workarounds were reset() and end(), but they move the array's internal pointer and can throw a notice when called on some expressions. Writing $array[array_key_first($array)] works, but it's long and awkward.

How it works

array_first(["single element"]); // "single element"
array_last(["single element"]); // "single element"

array_first([]); // NULL
array_last([]); // NULL

array_first([1 => 'a', 0 => 'b', 3 => 'c', 2 => 'd']); // 'a'
array_last([1 => 'a', 0 => 'b', 3 => 'c', 2 => 'd']); // 'd'

The functions follow the array's internal order, not the numeric order of the keys. An empty array returns null, which matches what array_find(), array_shift() and array_pop() do and works well with the ?? operator. If a value is a reference, you get the plain value back.

An older RFC tried to add these functions alongside the key functions, but that part failed, mostly over how to handle empty arrays. This RFC chose null and explains the reasoning.

The names leave out the word "value" on purpose, matching other array functions like array_find() and array_find_key().

What it means for existing code

If you defined your own global array_first() or array_last() function, it will clash with the new ones. You'll need to wrap it in a function_exists() check or remove it.

The vote

Accepted 35 to 0, clearing the two-thirds majority it needed. Voting closed on May 6, 2025, and both functions shipped in PHP 8.5.