What is the array_key_last method in PHP?

The array_key_last can be used to get the last key of the array.

Syntax

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

If the passed array doesn’t have any element, then it returns null.

Example

<?php
$arr = [
'ONE' => 1,
'TWO' => 2,
'THREE' => 3
];
$lastKey = array_key_last($arr);
echo "The last key of the array is :". $lastKey;
?>

In the code above, We have created an array and get the last key of the array using the array_key_last method.

Example 2

Let’s call array_key_last on an empty array:

<?php
$arr = [];
$lastKey = array_key_last($arr);
var_dump($lastKey);
?>

In the code above, we will get NULL as a result because the passed array is empty.

Free Resources