Arrays7 min readExamples run on PHP 8.5.10

array_key_last()

Learn how array_key_last() returns the final key of a PHP array, or null when it is empty, without moving the internal pointer, with examples and pitfalls.

Signature

array_key_last(array $array): int|string|null
Available since PHP 7.3.07.48.08.18.28.38.48.5All versions →
PHP array_key_last()

Purpose

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 int or a string when the array has at least one element;
  • null when 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.

Example
<?php
$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";
Output
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.

Example
<?php
$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));
Output
{"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".

Example
<?php
$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";
Output
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() or asort() 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

  1. Using if ($key) to detect an empty array. Compare with null using === instead, since 0 and "" are legitimate keys.
  2. 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.
  3. 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.
  4. Reading the value without checking. Writing $array[array_key_last($array)] on an empty array looks up the key null, which is treated as an empty string key, and causes an undefined-key warning. Check for null first.
  5. Passing a non-array value. A TypeError is thrown. Validate input or declare array types in your own functions.
  • array_key_first() returns the first key and also gives null for 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

Last key of a list and of an associative array
<?php
$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";
Output
2
"y"
Pointer is untouched and empty array gives null
<?php
$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));
Output
{"last":2,"current":20}
NULL
Insertion order, unset and key 0
<?php
$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";
Output
7
9
0