Array Functions

PHP array_key_last() Function

Topic: PHP Array ReferencePrev|Next


The array_key_last() function gets the last key of an array.

The following table summarizes the technical details of this function.

Return Value: Returns the last key of array if the array is not empty; NULL otherwise.
Version: PHP 7.3.0+


The basic syntax of the array_key_last() function is given with:


The following example shows the array_key_last() function in action.

// Sample array
$persons = array("Harry"=>18, "Clark"=>32, "John"=>24);
// Getting the last key from the persons array
echo array_key_last($persons); // Prints: John


The array_key_last() function accepts the following parameters.

Parameter Description
array Required. Specifies the array to be used.

More Examples

Here're some more examples showing how array_key_last() function basically works:

This function can also be used with the indexed array, as shown here:

// Creating an indexed array
$colors = array("red", "green", "blue");

// Getting the last index from the colors array
echo array_key_last($colors); // Prints: 2
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties