array_key_first and array_key_last are the correct PHP ways. essentially: $first = $array[array_key_first($array)] ?? null; $last = $array[array_key_last($array)] ?? null; Is it a little ugly? Yes, but it handles arrays not necessarily being indexed as a list in other languages would be, and doesn't change the state of the array in any way as reset and end would. Answer from Plastonick on reddit.com
🌐
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?

Discussions

For Beginners: How to get the first element of an array
Who the fuck would watch a 3 minutes long video for something this simple and straightforward ! More on reddit.com
🌐 r/PHP
16
0
December 30, 2019
How to get first value from array? - PHP - SitePoint Forums | Web Development & Design Community
Hello, I have a dynamically generated array which has data like following: Array ( [4] => 3 [3] => 1 ) How to get the first value from the array as I don’t know whats going to be the values. So as in this the first is 4 and it can be anything. How do I get the first value ? More on sitepoint.com
🌐 sitepoint.com
0
March 27, 2011
How to get the first element in the Associative array.
Forgot your password · By theITvideos September 2, 2010 in PHP Coding Help More on forums.phpfreaks.com
🌐 forums.phpfreaks.com
8
June 10, 2010
php - First element of array by condition - Stack Overflow
I am looking for an elegant way to get the first (and only the first) element of an array that satisfies a given condition. ... Both seems a little cumbersome. Does anyone have a cleaner solution? Am i missing a built-in php function? More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 - In this approach, the first element of the array is accessed directly by using the index 0. PHP arrays are zero-based, so $array[0] retrieves the first element.
🌐
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
March 15, 2024 - The output gives us the value of the first element of our original array and the original array: ... $myArray = [ 'red' => 'apple', 'yellow' => 'banana', 'green' => 'grapes' ]; // get the first element $firstFruit = array_shift($myArray); // output ...
🌐
Medium
medium.com › @inanbunyamin90 › how-to-get-the-first-item-from-an-array-in-php-d5a167a3b311
How to get the first item from an array in PHP? | by Bunyamin Inan | Medium
May 8, 2020 - Maybe this is one of the reasons that developers hate PHP, I don’t know 😕. ... If you are using PHP 7.3+ (and you should), the there is a built-in function array_key_first which lets you get the first item from an array.
🌐
Reddit
reddit.com › r/php › for beginners: how to get the first element of an array
For Beginners: How to get the first element of an array : r/PHP
December 30, 2019 - Sometimes $arr[0] is all you need, sometimes reset($arr) is all you need... array_values($arr)[0] ?? NULL is the most flexible one-liner I can think of off the top of my head, but with the caveat you mentioned that you're making a copy of the array so you may run into performance or scaling issues with it. +1 for being constructive. ... Man, you don't even know how to follow your own comment thread. You're extremely lost. ... Introducing the 100-million-row challenge in PHP!
Find elsewhere
🌐
HashBangCode
hashbangcode.com › article › finding-first-and-last-items-array-php
Finding The First And Last Items In An Array In PHP | #! code
The reset() function will move the pointer to the start of the array and return the first element. The current() function will return the current item in the array, which should be the first element if the array is newly initialised. Using the current() function can be unpredictable unless ...
🌐
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 ...
🌐
SitePoint
sitepoint.com › php
How to get first value from array? - PHP - SitePoint Forums | Web Development & Design Community
March 27, 2011 - Hello, I have a dynamically generated array which has data like following: Array ( [4] => 3 [3] => 1 ) How to get the first value from the array as I don’t know whats going to be the values. So as in this the …
🌐
W3Schools
w3schools.com › php › func_array_shift.asp
PHP array_shift() Function
<?php $a=array("a"=>"red","b"=>"green","c"=>"blue"); echo array_shift($a); print_r ($a); ?> Try it Yourself » · The array_shift() function removes the first element from an array, and returns the value of the removed element.
🌐
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
In that case you can use the ... array_values($arr)[0]; // Outputs: Apple ?> Alternativly, you can also use the reset() function to get the first element....
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-remove-first-element-from-an-array-in-php
How to Remove First Element from an Array in PHP? - GeeksforGeeks
July 23, 2025 - First, identify the key of the first element, then create a temporary array with this key and use array_diff_key() to remove it. ... <?php $array = ['a' => 'apple', 'b' => 'banana', 'c' => 'cherry']; // Getting the first key of the array $firstKey ...
🌐
Wyzant
wyzant.com › resources › ask an expert
Get the first element of an array in PHP? | Wyzant Ask An Expert
May 14, 2019 - I have an array: `array( 4 => 'apple', 7 => 'orange', 13 => 'plum' )` I would like to get the first element of this array. Expected result: <i>string</i> `apple` One requirement: *it cannot be done with passing by reference*, so `array_shift` is not a good solution.