Signature
array_key_last(array $array): int|string|nullPurpose
array_key_last() tells you which key sits at the end of an array. "Last" refers to the position in the array's current order, which is the order in which elements were inserted, not the numerically largest key and not the alphabetically greatest one. The function only reads the array. It does not move the internal array pointer, so it is safe to call in the middle of code that uses current(), next() or reset().
Before this function existed, people usually wrote end($array) followed by key($array). That works, but it changes the pointer and needs a variable it can take by reference. array_key_last() takes the array by value and gives the answer in a single call, which also lets you use it directly on the result of another function.
Signature
The function has been available since PHP 7.3 and is part of PHP 8.4.
Parameters and defaults
$array: the array to inspect. It is required and has no default value. Any array is accepted, whether it is a list, an associative array or a mix of integer and string keys.
Passing something that is not an array, such as a string or null, throws a TypeError, because the parameter is declared as array. In files that do not use strict types, the usual scalar coercion rules do not make a string into an array, so the error is always raised for non-array values.
Return value and failure cases
The function returns:
- the last key as an
intor astringwhen the array has at least one element; nullwhen the array is empty.
There is no exception for an empty array, so the null return is the only signal. Keys keep their original type: a key such as "7" is already stored as the integer 7 by PHP, so you get back 7.
Example 1: lists and associative arrays
On a list, the last key is the highest index. On an associative array, it is the key of the most recently added element.
$stack = ['a', 'b', 'c'];
echo json_encode(array_key_last($stack)), "\n";
$map = ['x' => 10, 'y' => 20];
echo json_encode(array_key_last($map)), "\n";2 "y"
Example 2: no pointer side effects and the empty array
The internal pointer stays where it was. Here next() moves it to the second element, and after calling array_key_last() current() still returns that second element. An empty array produces null, which var_dump() prints as NULL.
$items = [10, 20, 30];
next($items);
$last = array_key_last($items);
echo json_encode(['last' => $last, 'current' => current($items)]), "\n";
$empty = [];
var_dump(array_key_last($empty));{"last":2,"current":20}
NULL
Example 3: insertion order, unset and key 0
The result follows position, not magnitude. In the array below, key 7 was added last, so it is returned even though 9 is larger. After unset() removes it, key 9 becomes the last one. The final lines show that a single-element list returns 0, which is a valid key and must not be confused with "no key".
$list = [5 => 'a', 9 => 'b', 7 => 'c'];
echo json_encode(array_key_last($list)), "\n";
unset($list[7]);
echo json_encode(array_key_last($list)), "\n";
$zero = ['only'];
$key = array_key_last($zero);
echo json_encode($key === null ? 'empty' : $key), "\n";7 9 0
Edge cases
- Empty array: returns
null. Check this before using the key to read a value. - Key 0: a valid result that is falsy. A test like
if ($key)would wrongly treat it as missing. - Non-sequential keys: the function does not compute
count() - 1. After removals or with custom keys, the last key can differ from the count minus one. - Order changes: sorting functions such as
ksort()orasort()change positions, so the last key can change after sorting. - Nested arrays: only the top level is examined. A nested array is just the value of the last key.
- Copies: the argument is passed by value, so the function never modifies your array.
Common mistakes
- Using
if ($key)to detect an empty array. Compare withnullusing===instead, since0and""are legitimate keys. - Assuming the last key equals
count($array) - 1. This holds only for lists without gaps. It fails for arrays with string keys or removed elements. - Assuming the largest key is returned. The function reports position, not the maximum. Use
max(array_keys($array))if you actually need the largest integer key, and handle the empty case separately. - Reading the value without checking. Writing
$array[array_key_last($array)]on an empty array looks up the keynull, which is treated as an empty string key, and causes an undefined-key warning. Check fornullfirst. - Passing a non-array value. A
TypeErroris thrown. Validate input or declare array types in your own functions.
Related functions
array_key_first()returns the first key and also givesnullfor an empty array.end()moves the internal pointer to the last element and returns its value.key()returns the key at the current internal pointer position.array_keys()returns all keys as a list when you need more than the boundary ones.count()gives the number of elements, which is not always connected to the last key.
Recap
array_key_last() returns the key of the final element in an array's order, or null for an empty array. It leaves the internal pointer untouched, accepts only arrays, and returns keys as int or string. Always compare the result to null strictly, and remember that it reflects position rather than size or magnitude.
Tested examples
$stack = ['a', 'b', 'c'];
echo json_encode(array_key_last($stack)), "\n";
$map = ['x' => 10, 'y' => 20];
echo json_encode(array_key_last($map)), "\n";
2 "y"
$items = [10, 20, 30];
next($items);
$last = array_key_last($items);
echo json_encode(['last' => $last, 'current' => current($items)]), "\n";
$empty = [];
var_dump(array_key_last($empty));
{"last":2,"current":20}
NULL
$list = [5 => 'a', 9 => 'b', 7 => 'c'];
echo json_encode(array_key_last($list)), "\n";
unset($list[7]);
echo json_encode(array_key_last($list)), "\n";
$zero = ['only'];
$key = array_key_last($zero);
echo json_encode($key === null ? 'empty' : $key), "\n";
7 9 0