I'm frequently using the following to get the second to last value in an array:
$z=array_pop(array_slice($array,-2,1));
Am I missing a php function to do that in one go or is that the best I have?
I'm frequently using the following to get the second to last value in an array:
$z=array_pop(array_slice($array,-2,1));
Am I missing a php function to do that in one go or is that the best I have?
For numerically indexed, consecutive arrays, try $z = $array[count($array)-2];
Edit: For a more generic option, look at Artefecto's answer.
end($array);
$z = prev($array);
This is more efficient than your solution because it relies on the array's internal pointer. Your solution does an uncessary copy of the array.
Or here, should work.
$reverse = array_reverse( $array ); $z = $reverse[1];
I'm using this, if i need it :)