array_key_last

Returns the last key of an array without moving the internal array pointer.

Signature
array_key_last(array $array): int|string|null
  • Added in PHP 7.3.0

Parameters

ParameterTypeDescription
$arrayarrayAn array.

Return value

int|string|nullThe last key of the array, or null if the array is empty.

Errors

Passing a non-array argument throws a TypeError.

array_key_last() returns the key of the last element in insertion order, leaving the internal array pointer where it was. It replaces the old end($a); key($a); pair, which needed a variable and had a pointer side effect.

Do not assume count() - 1

For a clean list the last key equals count($array) - 1, but that stops being true the moment anything unsets an element or filters the array. Worse, PHP remembers the next free integer index: after unset($a[1]) a later $a[] = 'c' gets key 2, not 1. So a two-element array can have a last key of 2. Always ask rather than compute.

Empty arrays return null

As with array_key_first(), null means empty. Indexing with it produces an undefined array key warning, so check first.

Peek versus pop

array_pop() also gets you the last element, but it removes it and, as a bonus side effect, resets the array pointer. Use array_key_last() when you only want to look; use array_pop() when you want it gone. On PHP 8.5, array_last() returns the last value directly.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Append to the newest group in a grouped array without re-scanning it.
  • Read the most recent entry of a time-ordered log array to compare against a new one.
  • Detect the final iteration of a foreach so you can skip the trailing separator.

Frequently asked questions

What is the difference between array_key_last and end?

end() moves the internal pointer to the last element and returns its value; array_key_last() returns the last key and does not touch the pointer. Use array_key_last for a side-effect-free peek.

Is the last key always count($array) - 1?

Only for an untouched list. After unset() or array_filter the keys have gaps, and appending after an unset reuses the old high-water mark, so the last key can exceed count() - 1.

How do I get the last value instead of the last key?

Use $array[array_key_last($array)], or array_last($array) on PHP 8.5. Use array_pop() instead if you also want to remove it.

More examples

Detecting the final iteration

Comparing the current key against the precomputed last key prints the arrow separator only between items, giving alpha -> beta -> gamma with no trailing arrow.

Example · php
<?php
$parts = ['alpha', 'beta', 'gamma'];
$lastKey = array_key_last($parts);

foreach ($parts as $key => $part) {
    echo $part;
    echo $key === $lastKey ? "\n" : ' -> ';
}

Peek versus pop

array_key_last leaves the array untouched, so the count is still 3 after peeking. array_pop removes the element and the count drops to 2.

Example · php
<?php
$stack = ['a', 'b', 'c'];

echo 'peek: ', $stack[array_key_last($stack)], "\n";
echo 'count after peek: ', count($stack), "\n";

$popped = array_pop($stack);
echo 'pop: ', $popped, ' count after pop: ', count($stack), "\n";

Discussion

  • Be the first to comment on this lesson.