array_key_last() returns the last key without changing the array's internal pointer. It returns null for an empty array.
$settings = ['theme' => 'dark', 'language' => 'en'];
$lastKey = array_key_last($settings);
echo $lastKey;This is clearer than manually moving the internal array pointer just to obtain the last key.
key = array_key_last ($input_array);
| Parameter | DESCRIPTION |
|---|---|
| $input_array | Required : Input array for which last key is to be returned. |
$input=array(1,2,3,4,5);
echo array_key_last($input);
Output is here
4
Note that the first key is 0 , so the last key in above code is 4
$input=array('One' =>'First','Two'=>'Second','Three'=>'Third','Fourth');
echo var_dump(array_key_last($input));
The output is here
int(0)
<?php
$input = array(10, 20, 30, 40);
echo array_key_last($input); // Output: 3
?>
<?php
$input = array('a' => 'Apple', 'b' => 'Banana', 'c' => 'Cherry');
echo array_key_last($input); // Output: 'c'
?>
<?php
$emptyArray = array();
echo array_key_last($emptyArray); // Output: NULL
?>
<?php
$input = array('first' => 100, 'second' => 200, 'third' => 300);
unset($input['third']);
echo array_key_last($input); // Output: 'second'
?>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.