PHP 8.5: New array_first and array_last functions

Version8.5
TypeNew Feature

PHP 8.5 adds two new functions for retrieving the first and last values of an array. These functions complement the array_key_first and array_key_last functions added in PHP 7.3.

  • array_first: Retrieve the first value from a given array; null if the array is empty.
  • array_last: Retrieve the last value from a given array; null if the array is empty.

On null values

Note that these functions return null if the array is empty. However, null itself can be a valid array value.

array_first

The array_first function returns the first array value of a given array. On a list-arrays, this essentially the key 0 value. However, array_first returns the first value even on associative arrays.

/**
  * Returns the first value of a given array.
  *
  * @param array $array The array to get the first value of.
  * @return mixed First value of the array, or null if the array is
  *   empty. Note that null itself can also be a valid array value.
  */
function array_first(array $array): mixed {}

array_first usage examples

array_first([1, 2, 3]); // 1
array_first([2, 3]); // 2
array_first(['a' => 2, 'b' => 1]); // 2
array_first([null, 2, 3]); // null
array_first([]); // null
array_first([$obj, 2, 3]); // $obj
array_first([1])); // 1
array_first([true]); // true

array_last

array_last function returns the last array value of a given array.

/**  
 * Returns the first value of a given array. * * @param array $array The array to get the first value of.  
 * @return mixed First value of the array, or null if the array is  
 *   empty. Note that null itself can also be a valid array value.
 */
function array_last(array $array): mixed {  
  return empty($array) ? null : $array[array_key_last($array)];  
}

array_last usage examples

array_last([1, 2, 3]); // 3
array_last([2, 3]); // 3
array_last(['a' => 2, 'b' => 1]); // 1
array_last([2, 3, null]); // null
array_last([]); // null
array_last([2, 3, $obj]); // $obj
array_last([1])); // 1
array_last([true]); // true

User-land PHP Polyfill

It is possible to trivially polyfill the new two functions using the array_key_first and array_key_last functions added in PHP 7.3.

function array_first(array $array): mixed {  
  return $array === [] ? null : $array[array_key_first($array)];  
}
function array_last(array $array): mixed {  
  return $array === [] ? null : $array[array_key_last($array)];  
}

Alternately, the polyfills/array-first-array-last package provides the polyfills for PHP 7.3 through PHP 8.4.

composer require polyfills/array-first-array-last

Backward Compatibility Impact

Existing PHP applications that declare their own array_first and array_last functions will cause fatal errors due to the attempt to redeclare these functions. These applications either remove their own declarations, rename the functions, or add a namespace.

For other applications, this change does not cause any backward compatibility issues.

Further, it is possible to polyfill these functions.


array_first array_last RFC Discussion Implementation