array_key_last
Returns the last key of an array.
Syntax
array_key_last(array $array): int|string|nullParameters
| Parameter | Type | Description |
|---|---|---|
| $array | array | The input array. |
Return value
int|string|nullThe last key, or `null` for an empty array.
Examples
The last key and element:
<?php
$steps = ['cart' => 1, 'address' => 2, 'payment' => 3];
$last = array_key_last($steps);
echo $last, ' ', $steps[$last];Output:
payment 3Notes
Unlike end(), the function does not move the array's internal pointer.
See also
- array_key_first — Returns the first key of an array.
- end — Returns the last element of an array.
- array_pop — Removes the last element of an array and returns it.
In the tutorial
- Array functions — Processing arrays in PHP without loops — array_map, array_filter, array_reduce, searching and slicing.