array_first()
Gets the first element of the array. Polyfill for the function added in PHP 8.5
The value is taken from the beginning of the array in the order in which the elements are located in the array. The key of the first element is not used — the function receives only the value.
If the array is empty, it will return null.
Polyfill for the built-in PHP function array_first().
No Hooks.
Returns
Mixed|null.
mixed— value of the first element of the array.null— if the array is empty.
Usage
array_first( $array );
- $array(array) (required)
Array from which you need to get the first element.
The parameter must be an array. If you pass a value of another type, PHP will throw a type error.
Examples
Changelog
| Since 6.9.0 | Introduced. |
array_first() array first code WP 7.0.2
function array_first( array $array ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
if ( empty( $array ) ) {
return null;
}
foreach ( $array as $value ) {
return $value;
}
}