A solution would be to use a combination of end and key (quoting) :

  • end() advances array 's internal pointer to the last element, and returns its value.
  • key() returns the index element of the current array position.

So, a portion of code such as this one should do the trick :

$array = array(
    'first' => 123,
    'second' => 456,
    'last' => 789, 
);

end($array);         // move the internal pointer to the end of the array
array);  // fetches the key of the element pointed to by the internal pointer

var_dump($key);

Will output :

string 'last' (length=4)

i.e. the key of the last element of my array.

After this has been done the array's internal pointer will be at the end of the array. As pointed out in the comments, you may want to run reset() on the array to bring the pointer back to the beginning of the array.

Answer from Pascal MARTIN on Stack Overflow
🌐
PHP
php.net › manual › en › function.array-key-last.php
PHP: array_key_last - Manual
<?php if( !function_exists('array_key_last') ) { function array_key_last(array $array) { if( !empty($array) ) return key(array_slice($array, -1, 1, true)); } } // Bonus if (!function_exists('array_key_first')) { function array_key_first(array $arr) { foreach($arr as $key => $unused) return $key; } } ?>
🌐
FlatCoding
flatcoding.com › home › php array_key_last function: how it works with examples
PHP array_key_last Function: How it Works with Examples - FlatCoding
September 20, 2025 - The array_key_last gives you the last key in an array and works with numeric and associative arrays in PHP.
People also ask

How is array_key_last different from array_key_first?
  • array_key_last gives the last key of an array.
  • array_key_first gives the first key of an array.

 100, "b" => 200];
echo array_key_first($data); // Output: a
echo array_key_last($data);  // Output: b
?>
🌐
flatcoding.com
flatcoding.com › home › php array_key_last function: how it works with examples
PHP array_key_last Function: How it Works with Examples - FlatCoding
🌐
ZetCode
zetcode.com › php-array › array-key-last
PHP array_key_last - Get Last Array Key in PHP
The PHP array_key_last function returns the last key of an array without affecting the internal array pointer.
🌐
OnlinePHP
onlinephp.io › array-key-last › manual
array_key_last - OnlinePHP.io Example
Returns the last key of array if the array is not empty; null otherwise.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-array_%e2%80%8bkey_%e2%80%8blast-function
PHP array_​key_​last() Function - GeeksforGeeks
July 22, 2022 - The array_​key_​last() function is an inbuilt function in PHP that is used to get the last key of an array.
🌐
W3Docs
w3docs.com › php
How to get last key in an array? - PHP
<?php $array = [1, 2, 3, 4, 5]; $last_key = end($array); echo $last_key; // Outputs 5
Find elsewhere
🌐
W3Schools
w3schools.com › php › func_array_end.asp
PHP end() Function
MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data · PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat Parser PHP DOM Parser · AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll · PHP Examples PHP Compiler PHP Quiz PHP Exercises PHP Server PHP Syllabus PHP Study Plan PHP Certificate ... array() array_change_key_case() array_chunk() array_column() array_combine() array_co
🌐
PHP
wiki.php.net › rfc › array_key_first_last
PHP: rfc:array_key_first_last
// usage of an associative array $array = ['a' => 1, 'b' => 2, 'c' => 3]; $firstKey = array_key_first($array); $lastKey = array_key_last($array); assert($firstKey === 'a'); assert($lastKey === 'c'); // usage of a numeric array $array = [1 => 'a', 2 => 'b', 3 => 'c']; $firstKey = array_key_first($array); $lastKey = array_key_last($array); assert($firstKey === 1); assert($lastKey === 3); // usage of an empty array $array = []; $firstKey = array_key_first($array); $lastKey = array_key_last($array); assert($firstKey === null); assert($lastKey === null);
🌐
PHP.Watch
php.watch › versions › 7.3 › array-key-first-last
Introduced `array_key_first()` and `array_key_last()` functions - PHP 7.3 • PHP.Watch
The RFC also proposed array_value_first() and array_value_last() functions, but this portion of the RFC didn't get voted. If you cannot immediately upgrade to PHP 7.3, you can get the same functionality with the two polyfills below. if (!function_exists('array_key_first')) { function array_key_first(array $array) { foreach ($array as $key => $value) { return $key; } } } if (!function_exists('array_key_last')) { function array_key_last(array $array) { end($array); return key($array); } }
🌐
WP Kama
wp-kama.com › all functions › functions › other functions › helper functions
array_key_last() – Gets the last key of the given array.
March 16, 2022 - Use array_key_first() when you need to get the first key of the array. 1 time — 0.000003 sec (speed of light) | 50000 times — 0.0001 sec (speed of light) ... String|Int|null. The last key of the array, if the array is not empty; null otherwise.
🌐
Envato Tuts+
code.tutsplus.com › home › coding fundamentals
Get the First and Last Elements of an Array in PHP | Envato Tuts+
April 19, 2021 - This makes it similar to the reset() function we discussed in the previous section. Just like array_key_first(), there is also a corresponding array_key_last() function which gives you the last key of the array without modifying it in any way.
🌐
OnlinePHP
onlinephp.io › array-key-last
array_key_last - Online Tool
PHP Functions · Arrays · array_key_last · Execute array_key_last with this online tool array_key_last() - Gets the last key of an array · Array Key Last Online Tool · Manual · Code Examples · Array Key Last Online Tool · Manual · Code ...
🌐
PHP.Watch
php.watch › codex › array_key_last
array_key_last Function • PHP.Watch
Returns the last key of $array if the array is not empty; null otherwise.