PHP 8 reference · Array Functions

PHP array_key_last() Function

Return the last key of an array without moving its internal pointer, or null for an empty array. Added in 7.3.

PHP 8 Since 7.3

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_key_last(array $array): int|string|null

Parameters

ParameterDescription
array The input array.

Return value

The last key, or null.

Examples

Last key and valueTry it yourself
<?php
$steps = ["start", "middle", "end"];
echo array_key_last($steps), "\n";
echo $steps[array_key_last($steps)], "\n";

Output

2
end
Spot the last item in a loopTry it yourself
<?php
$items = ["a", "b", "c"];
$last = array_key_last($items);
foreach ($items as $i => $item) {
    echo $item, $i === $last ? "\n" : ", ";
}

Output

a, b, c

Version history

VersionChange
PHP 7.3Added

Was this page helpful?