Community addition: Since this answer was written, new functions have been added to PHP, suggested in respective answers: array_key_first() (PHP 7.3+) and, finally, array_first() (PHP 8.5+).

Original answer, but costly (O(n)):

array_shift(array_values($array));

In O(1):

array_pop(array_reverse($array));

Other use cases, etc...

If modifying (in the sense of resetting array pointers) of $array is not a problem, you might use:

reset($array);

This should be theoretically more efficient, if a array "copy" is needed:

array_shift(array_slice($array, 0, 1));

With PHP 5.4+ (but might cause an index error if empty):

array_values($array)[0];
Answer from blueyed on Stack Overflow
🌐
PHP
php.net › manual › en › function.array-search.php
PHP: array_search - Manual
array_search — Searches the array for a given value and returns the first corresponding key if successful
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-get-the-first-element-of-an-array-in-php
How to get the First Element of an Array in PHP? - GeeksforGeeks
July 11, 2025 - ... <?php // PHP program to access the first // element of the array $array = array( 33 => 'geeks', 36 => 'for', 42 => 'computer' ); foreach($array as $name) { echo $name; // Break loop after first iteration break; } ?>
🌐
Tutorial Republic
tutorialrepublic.com › faq › how-to-get-the-first-element-of-an-array-in-php.php
How to Get the First Element of an Array in PHP
This function returns the current element in an array, which is the first element by default unless you've re-positioned the array pointer, otherwise use the reset() function. Here's an example: ... <?php $arr = array(3 => "Apple", 5 => "Ball", ...
🌐
W3Docs
w3docs.com › php
How to Get the First Element of an Array in PHP
<?php $array = [4 => 'apple', 7 ... If you want to find the first key of the array instead of the value you should use the array_keys function....
🌐
W3Schools
w3schools.com › php › func_array_search.asp
PHP array_search() Function
The array_search() function search an array for a value and returns the key. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, ...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › php tutorial › php get first element of array
PHP Get First Element of Array | Guide to PHP Get First Element of Array
April 1, 2023 - To get the first value we can directly ... are in build. In PHP, to get the first element we have methods as, [pass_index], reset () method, array_values() method....
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Sentry
sentry.io › sentry answers › php › how can i get the first element of an array in php?
How can I get the first element of an array in PHP? | Sentry
$myArray = [ 'red' => 'apple', 'yellow' => 'banana', 'green' => 'grapes' ]; // get the first element $firstFruit = array_shift($myArray); // output echo $firstFruit .PHP_EOL; print_r($myArray);
Find elsewhere
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
Get the First and Last Elements of an Array in PHP | Envato Tuts+
April 19, 2021 - One of the most efficient ways of getting the first element of a numerical or associative array in PHP is to use the reset() function. This function sets the internal pointer of the array to its first element and returns the value of the first ...
🌐
GitHub
gist.github.com › bombsimon › c2592efb77d169d65cdae6902791a0bb
Find first occurrence of key in PHP array · GitHub
Find first occurrence of key in PHP array. GitHub Gist: instantly share code, notes, and snippets.
🌐
Reddit
reddit.com › r/phphelp › what is the idiomatic way to get first and last array element?
r/PHPhelp on Reddit: What is the idiomatic way to get first and last array element?
January 24, 2024 -

I do not do much PHP development except for some small WordPress things. I often need to get the first or last element from an array that also may be empty. To my knowledge there are two approaches that are safe.

1: $first = $array[0] ?? null; $last = $array[count( $array ) - 1] ?? null;

2: $first = reset( $array ); $last = end( $array );

Are there others? Is there a community consensus? WordPress specific consensus?

🌐
PHP
wiki.php.net › rfc › array_find
PHP: rfc:array_find
array_find returns the value of the first element for which the $callback returns true.