PHP array_key_last() Function
Topic: PHP Array ReferencePrev|Next
Description
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+ |
Syntax
The basic syntax of the array_key_last()
function is given with:
array_key_last(array);
The following example shows the array_key_last()
function in action.
Example
Run this code »<?php
// 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
?>
Parameters
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:
Example
Run this code »<?php
// Creating an indexed array
$colors = array("red", "green", "blue");
// Getting the last index from the colors array
echo array_key_last($colors); // Prints: 2
?>