The array_key_last() function is an inbuilt function in PHP that is used to get the last key of an array. This function returns the last key without affecting the internal array pointer.
Syntax:
int|string|null array_key_last(array $array)
Parameters: This function accepts single parameter $array that holds the array element.
Return Value: This function returns the last key of the array if the array is not empty, and null otherwise.
Example 1:
PHP
<?php
$arr = array ();
var_dump(array_key_last( $arr ));
$arr1 = array ( "10" , "20" , "30" , "40" , "50" );
var_dump(array_key_last( $arr1 ));
?>
|
Output:
NULL
int(4)
Example 2:
PHP
<?php
$arr = array (
'Geeks' => "HTML" ,
'GFG' => "CSS" ,
'Geek' => "JavaScript" ,
'G4G' => "PHP"
);
var_dump(array_key_last( $arr ));
$arr1 = array (
5 => "geeks" ,
10 => "GFG"
);
var_dump(array_key_last( $arr1 ));
?>
|
Output:
string(3) "G4G"
int(10)
Reference: https://www.php.net/manual/en/function.array-key-last.php
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Jul, 2022
Like Article
Save Article