php - What's the best way to get the last element of an array without
deleting it?
itemprop="text">
Ok,
I know all about rel="noreferrer">array_pop(), but that deletes the last element. What's the best way to get the last element of an array without deleting it?
$array = array('a', 'b', 'c', 'd'); unset($array[2]); echo $array[sizeof($array) - 1]; // Output: PHP Notice: Undefined offset: 2 in - on line 4
itemprop="text">
class="normal">Answer
The many answers in this thread present us with many different options. To be able to choose from them I needed to understand their behavior and performance. In this answer I will share my findings with you, benchmarked against PHP versions 5.6.38, 7.2.10 and 7.3.0RC1 ( href="https://wiki.php.net/todo/php73" rel="noreferrer">expected Dec 13 2018).
For testing I will use the 5.6.38, 7.2.10 and 7.3.0RC1 rel="noreferrer">PHP docker containers like:
sudo docker run -it --rm php:5.6.38-cli-stretch php -r '<< HERE>>>'
Each combination of the above listed <s and <>s will be run on all versions of PHP. For each test run the following code snippet is used:
<> error_reporting(E_ALL); <
For each run this will var_dump the last retrieved last value of the test input and print the average duration of one iteration href="https://en.wikipedia.org/wiki/International_System_of_Units#Prefixes" rel="noreferrer">in femtoseconds (0.000000000000001th of a second).
The above mentioned Fatal, Warning and Notice codes translate as:
F1 = Fatal error: Call to undefined function array_key_last() in Command line code on line 1 F2 = Fatal error: Uncaught Error: Call to undefined function array_key_last() in Command line code:1 W1 = Warning: array_slice() expects parameter 1 to be array, null given in Command line code on line 1 W2 = Warning: array_values() expects parameter 1 to be array, null given in Command line code on line 1 W3 = Warning: array_pop() expects parameter 1 to be array, null given in Command line code on line 1 W4 = Warning: end() expects parameter 1 to be array, null given in Command line code on line 1 W5 = Warning: reset() expects parameter 1 to be array, null given in Command line code on line 1 W6 = Warning: array_keys() expects parameter 1 to be array, null given in Command line code on line 1 W7 = Warning: count(): Parameter must be an array or an object that implements Countable in Command line code on line 1 W8 = Warning: array_key_last() expects parameter 1 to be array, null given in Command line code on line 1
N1 = Notice: Undefined offset: 0 in Command line code on line 1 N2 = Notice: Only variables should be passed by reference in Command line code on line 1 N3 = Notice: Undefined offset: -1 in Command line code on line 1 N4 = Notice: Undefined index: in Command line code on line 1
Based on this output I draw the following conclusions:
newer versions of PHP perform better with the exception of these options that became significantly slower:
these options should only be used for auto-indexed arrays:
option .7.$x = $array[count($array)-1]; (due to use of count)
option .9.$x = $array[] = array_pop($array); (due to assigning value losing original key)
this option does not preserve the array's internal pointer
option .5.$x = end($array); reset($array);
this option is an attempt to modify option .5. to preserve the array's internal pointer (but sadly it does not scale well for very large arrays)
option .6.$x = end((array_values($array)));
the new array_key_last function seems to have none of the above mentioned limitations with the exception of still being an RC at the time of this writing (so use the RC or await it's release Dec 2018):
A bit depending on whether href="https://stackoverflow.com/a/41387093/2799887">using the array as stack or as queue you can make variations on option 9.
No comments:
Post a Comment