$lastEl = array_values(array_slice($array, -1))[0];
So I understand array_values
and array_slice
but I'm not sure what the indicator on the end is doing to it.
Can somebody help?
$lastEl = array_values(array_slice($array, -1))[0];
So I understand array_values
and array_slice
but I'm not sure what the indicator on the end is doing to it.
Can somebody help?
If you break it down...
$lastEl = array_values(array_slice($array, -1))[0];
Firstly...
array_slice($array, -1)
This is taking part of the array the -1 means from the end of the array, so this takes the last element and returns an array.
array_values(array_slice($array, -1))
This returns the values from the returned array - again as an array.
[0]
Takes the first element of the resultant array, as so far the result has been an array of 1 element (the last one) it converts the result from an array to the value of the last element of the array.
So an example...
$array = [1,2,3,4,5];
$lastEl = array_values(array_slice($array, -1))[0];
var_dump($lastEl);
will output
/home/nigel/workspace2/Test/t2.php:8:
int(5)