array_key_last(): to get the last key of an array

key = array_key_last ($input_array);
ParameterDESCRIPTION
$input_arrayRequired : Input array for which last key is to be returned.
arrays_key_last() function returns the last key of the array. If the array is empty then NULL is returned.
PHP 7.3.0 Supported by PHP 7.3.0 and above

Example 1

$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

Example 2

$input=array('One' =>'First','Two'=>'Second','Three'=>'Third','Fourth');
echo var_dump(array_key_last($input));
The output is here
int(0)

Example 3: Using array_key_last() with Numeric Array

<?php
$input = array(10, 20, 30, 40);
echo array_key_last($input);  // Output: 3
?>

Example 4: Using array_key_last() with Associative Array

<?php
$input = array('a' => 'Apple', 'b' => 'Banana', 'c' => 'Cherry');
echo array_key_last($input);  // Output: 'c'
?>

Example 5: Handling an Empty Array

<?php
$emptyArray = array();
echo array_key_last($emptyArray);  // Output: NULL
?>

Example 6: Using array_key_last() After Array Modifications

<?php
$input = array('first' => 100, 'second' => 200, 'third' => 300);
unset($input['third']);
echo array_key_last($input);  // Output: 'second'
?>

Returns the First Key
Array REFERENCE
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer