Hi Reader's,
Welcome to FindNerd, today we are going to discuss how to get last key in an array in PHP ?
we need in some cases the last key or last element of an array in PHP.
So We are using array_slice() function for getting only the last element of the array.
It is very easy and fast function for getting last element of the array
you can see bellow example:
<?php
//here define a variable of array
$data = array(
'Emp_name' => 'mac',
'Emp_age' => 35,
'Emp_dep' => 'account',
);
//here define a variable and call array_slice() to get last element of the array
$last_key = key( array_slice( $data, -1, 1, TRUE ) );
//here print $last_key variable
echo $last_key;
?>
output will come following of above example
Emp_dep
0 Comment(s)