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
Syntax
array_key_last(array $array): int|string|null
Parameters
| Parameter | Description |
|---|---|
array |
The input array. |
Return value
The last key, or null.
Examples
<?php
$steps = ["start", "middle", "end"];
echo array_key_last($steps), "\n";
echo $steps[array_key_last($steps)], "\n";
Output
2 end
<?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
| Version | Change |
|---|---|
| PHP 7.3 | Added |
Related
array_key_first()· Return the first key of an array without moving its internal pointer, or null for an empty array. Added in 7.3.array_last()· Return the last value of an array, or null if it's empty, without touching the internal pointer. Added in PHP 8.5.count()· Return the number of elements in an array or Countable object. With COUNT_RECURSIVE it also counts nested elements.- PHP Arrays · tutorial
- PHP Array Functions · tutorial