获取数组的最后一个元素而不删除它的最佳方法是什么? [英] What's the best way to get the last element of an array without deleting it?

查看:41
本文介绍了获取数组的最后一个元素而不删除它的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,

我对array_pop()了如指掌,但这会删除最后一个元素.获取数组的最后一个元素而不删除它的最佳方法是什么?

这是一个奖励:

$array = array('a' => 'a', 'b' => 'b', 'c' => 'c');

甚至

$array = array('a', 'b', 'c', 'd');未设置($array[2]);回声 $array[sizeof($array) - 1];//输出:PHP 注意:未定义偏移量:2 in - 第 4 行

解决方案

该线程中的许多答案为我们提供了许多不同的选择.为了能够从中选择,我需要了解他们的行为和表现.在这个答案中,我将与您分享我的发现,以 PHP 版本 5.6.387.2.107.3.0RC1(预计 2018 年 12 月 13 日).

我将测试的选项(<s)是:

  • option .1. $x = array_values(array_slice($array, -1))[0]; (根据 rolacja 的建议)
  • option .2. $x = array_slice($array, -1)[0]; (按照Stoutie的建议)
  • option .3. $x = array_pop((array_slice($array, -1))); (按照rolacja的建议)
  • option .4. $x = array_pop((array_slice($array, -1, 1))); (按照Westy92的建议)
  • option .5. $x = end($array);reset($array);(根据建议Iznogood)
  • option .6. $x = end((array_values($array))); (按照TecBrat的建议)
  • option .7. $x = $array[count($array)-1]; (根据 Mirko Pagliai 的建议)
  • option .8. $keys = array_keys($array);$x = $array[$keys[count($keys)-1]]; (建议来自 thrau)
  • option .9. $x = $array[] = array_pop($array); (按照建议 来自 user2782001)
  • 选项 10. $x = $array[array_key_last($array)]; (按照Quasimodo的克隆的建议;每个PHP可用7.3)

(提到的函数:array_key_last, array_keys , array_pop , array_slice , array_values , count , end , reset)

要结合的测试输入(<>s):

  • null = $array = null;
  • = $array = [];
  • last_null = $array = ["a","b","c",null];
  • auto_idx = $array = ["a","b","c","d"];
  • shuffle = $array = [];$array[1] = "a";$array[2] = "b";$array[0] = "c";
  • 100 = $array = [];for($i=0;$i<100;$i++) { $array[] = $i;}
  • 100000 = $array = [];for($i=0;$i<100000;$i++) { $array[] = $i;}

为了测试,我将使用 5.6.387.2.107.3.0RC1 PHP docker 容器 像:

sudo docker run -it --rm php:5.6.38-cli-stretch php -r '<<<此处代码>>>'

上面列出的 <s 和 <>s 的每个组合都将在所有版本上运行PHP的.对于每次测试运行,使用以下代码片段:

<<输入代码>>错误报告(E_ALL);<<选项代码>>错误报告(0);$before=microtime(TRUE);for($i=0;$i<100;$i++){echo ".";for($j=0;$j<100;$j++){ <<option code>>}};$after=microtime(TRUE);回声\n";var_dump($x);echo round(($after-$before)/(100*100)*1000*1000*1000);

对于每次运行,这将 var_dump 最后检索到的测试输入的最后一个值并打印一次迭代的平均持续时间 以飞秒为单位(0.000000000000001 秒).

结果如下:

/============================================================================================================================================================================================================================================================================================================================================================================================================================\||||测试输入 - 5.6 .3 8 ||测试输入 - 7.2 .1 0 ||测试输入 - 7.3 .0 R C 1 ||||||空|空|last_null |auto_idx |洗牌 |100 |100000 ||空|空|last_null |auto_idx |洗牌 |100 |100000 ||空|空|last_null |auto_idx |洗牌 |100 |100000 ||||============================选项 - 错误==========================++==============+================+================+================+================+================+================++================+================+================+================+================+================+==================++==============+================+==================+==============+================+==================+================<|||1. $x = array_values(array_slice($array, -1))[0];||W1 + W2 |N1 |- |- |- |- |- ||W1 + W2 |N1 |- |- |- |- |- ||W1 + W2 |N1 |- |- |- |- |- ||||2. $x = array_slice($array, -1)[0];||W1 |N1 |- |- |- |- |- ||W1 |N1 |- |- |- |- |- ||W1 |N1 |- |- |- |- |- ||||3. $x = array_pop((array_slice($array, -1)));||W1 + W3 |- |- |- |- |- |- ||W1 + N2 + W3 |氮气 |氮气 |氮气 |氮气 |氮气 |N2 ||W1 + N2 + W3 |氮气 |氮气 |氮气 |氮气 |氮气 |N2 ||||4. $x = array_pop((array_slice($array, -1, 1)));||W1 + W3 |- |- |- |- |- |- ||W1 + N2 + W3 |氮气 |氮气 |氮气 |氮气 |氮气 |N2 ||W1 + N2 + W3 |氮气 |氮气 |氮气 |氮气 |氮气 |N2 ||||5. $x = end($array);重置($数组);||W4 + W5 |- |- |- |- |- |- ||W4 + W5 |氮气 |氮气 |氮气 |氮气 |氮气 |N2 ||W4 + W5 |- |- |- |- |- |- ||||6. $x = end((array_values($array)));||W2 + W4 |- |- |- |- |- |- ||W2 + N2 + W4 |- |- |- |- |- |- ||W2 + N2 + W4 |氮气 |氮气 |氮气 |氮气 |氮气 |N2 ||||7. $x = $array[count($array)-1];||- |N3 |- |- |- |- |- ||W7 |N3 |- |- |- |- |- ||W7 |N3 |- |- |- |- |- ||||8. $keys = array_keys($array);$x = $array[$keys[count($keys)-1]];||W6 |N3 + N4 |- |- |- |- |- ||W6 + W7 |N3 + N4 |- |- |- |- |- ||W6 + W7 |N3 + N4 |- |- |- |- |- ||||9. $x = $array[] = array_pop($array);||W3 |- |- |- |- |- |- ||W3 |- |- |- |- |- |- ||W3 |- |- |- |- |- |- ||||10. $x = $array[array_key_last($array)];||F1 |F1 |F1 |F1 |F1 |F1 |F1 ||F2 |F2 |F2 |F2 |F2 |F2 |F2 ||W8 |N4 |F2 |F2 |F2 |F2 |F2 ||||======================== 选项 - 检索到的值====================================+==================+==============+================+==================+================+===============++================+==================================+================+================+================+================++================+================+================+================+================+================+================<|||1. $x = array_values(array_slice($array, -1))[0];||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||||2. $x = array_slice($array, -1)[0];||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||||3. $x = array_pop((array_slice($array, -1)));||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||||4. $x = array_pop((array_slice($array, -1, 1)));||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||||5. $x = end($array);重置($数组);||空 |布尔(假)|空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||空 |布尔(假)|空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||空 |布尔(假)|空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||||6. $x = end((array_values($array)));||空 |布尔(假)|空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||空 |布尔(假)|空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||空 |布尔(假)|空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||||7. $x = $array[count($array)-1];||空 |空 |空 |字符串(1) "d" |字符串(1) "b" |整数(99) |int(99999) ||空 |空 |空 |字符串(1) "d" |字符串(1) "b" |整数(99) |int(99999) ||空 |空 |空 |字符串(1) "d" |字符串(1) "b" |整数(99) |int(99999) ||||8. $keys = array_keys($array);$x = $array[$keys[count($keys)-1]];||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||||9. $x = $array[] = array_pop($array);||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||空 |空 |空 |字符串(1) "d" |字符串(1) "c" |整数(99) |int(99999) ||||10. $x = $array[array_key_last($array)];||不适用 |不适用 |不适用 |不适用 |不适用 |不适用 |不适用 ||不适用 |不适用 |不适用 |不适用 |不适用 |不适用 |不适用 ||不适用 |不适用 |不适用 |不适用 |不适用 |不适用 |不适用 ||||==================选项 - 每次迭代飞秒================++================+================+================+================+================+================+==============++================+==================+================+================+==================+==================================================+==============+================+==================+================+===============+================<|||1. $x = array_values(array_slice($array, -1))[0];||803 |第466话390 |第384话第373话第764话1.046.642 ||第691话第252话101 |128 |93 |170 |89.028 ||第695话第235话90 |97 |95 |188 |87.991 ||||2. $x = array_slice($array, -1)[0];||第414话第349话第252话第248话第246话604 |1.038.074 ||第373话第249话85 |91 |90 |164 |90.750 ||第367话第224话78 |85 |80 |155 |86.141||||3. $x = array_pop((array_slice($array, -1)));||第724话第228话第323话第318话350 |第673话1.042.263 ||988 |第285话第309话第317话第331话401 |88.363 ||第877话第266话第298话300 |第326话403 |87.279 ||||4. $x = array_pop((array_slice($array, -1, 1)));||第734话第266话第358话第356话第349话第699话1.050.101 ||第887话第288话第316话第322话第314话408 |88.402 ||935 |第268话第335话第315话第313话403 |86.445 ||||5. $x = end($array);重置($数组);||第715话186 |185 |180 |176 |185 |第172话第674话73 |69 |70 |66 |65 |70 ||第693话65 |85 |74 |68 |70 |69 ||||6. $x = end((array_values($array)));||第877话205 |320 |第337话304 |2.901 |7.921.860 ||948 |300 |第336话308 |第309话第509话29.696.951 ||946 |第262话301 |第309话302 |第499话29.234.928 ||||7. $x = $array[count($array)-1];||123 |300 |137 |139 |143 |140 |第144话第312话第218话48 |53 |45 |47 |第51话第296话第217话46 |44 |53 |53 |55 ||||8. $keys = array_keys($array);$x = $array[$keys[count($keys)-1]];||第494话第593话第418话第435话第399话3.873 |12.199.450 ||第665话407 |103 |109 |114 |第431话30.053.730 ||第647话第445话91 |95 |96 |第419话30.718.586 ||||9. $x = $array[] = array_pop($array);||186 |178 |175 |188 |180 |181 |第186话83 |78 |75 |71 |74 |69 |第83话71 |64 |70 |64 |68 |69 |第81话||10. $x = $array[array_key_last($array)];||不适用 |不适用 |不适用 |不适用 |不适用 |不适用 |不适用 ||不适用 |不适用 |不适用 |不适用 |不适用 |不适用 |不适用 ||第370话第223话49 |52 |61 |57 |52 ||\==========================================================================================================================================================================================================================================================================================================================================================================================================================/

上面提到的Fatal、Warning和N提示代码翻译为:

F1 = 致命错误:在第 1 行的命令行代码中调用未定义的函数 array_key_last()F2 = 致命错误:未捕获的错误:在命令行代码中调用未定义的函数 array_key_last():1W1 = 警告:array_slice() 期望参数 1 为数组,在第 1 行的命令行代码中给出为空W2 = 警告:array_values() 期望参数 1 为数组,在第 1 行的命令行代码中给出空值W3 = 警告:array_pop() 期望参数 1 为数组,在第 1 行的命令行代码中给出为空W4 = 警告:end() 期望参数 1 为数组,在第 1 行的命令行代码中给出为空W5 = 警告:reset() 期望参数 1 为数组,在第 1 行的命令行代码中给出为空W6 = 警告:array_keys() 期望参数 1 为数组,在第 1 行的命令行代码中给出为空W7 = 警告:count():参数必须是一个数组或一个对象,该对象在第 1 行的命令行代码中实现了 CountableW8 = 警告:array_key_last() 期望参数 1 为数组,在第 1 行的命令行代码中给出为空N1 = 注意:未定义偏移量:第 1 行命令行代码中的 0N2 = 注意:在第 1 行的命令行代码中,只应通过引用传递变量N3 = 注意:未定义偏移:-1 在第 1 行的命令行代码中N4 = 注意:未定义索引:在第 1 行的命令行代码中

基于此输出,我得出以下结论:

  • 较新版本的 PHP 性能更好,除了这些选项变得明显变慢:
    • option .6. $x = end((array_values($array)));
    • option .8. $keys = array_keys($array);$x = $array[$keys[count($keys)-1]];
  • 这些选项最适合非常大的数组:
    • option .5. $x = end($array);重置($array);
    • option .7. $x = $array[count($array)-1];
    • option .9. $x = $array[] = array_pop($array);
    • 选项 10. $x = $array[array_key_last($array)];(自 PHP 7.3 起)
  • 这些选项应该仅用于自动索引数组:
    • option .7. $x = $array[count($array)-1];(由于使用了计数)
    • option .9. $x = $array[] = array_pop($array);(由于赋值丢失了原来的key)
  • 这个选项不保留数组的内部指针
    • option .5. $x = end($array);重置($array);
  • 这个选项试图修改选项 .5. 以保留数组的内部指针(但遗憾的是它不能很好地扩展非常大的数组)
    • option .6. $x = end((array_values($array)));
  • 新的 array_key_last 函数似乎没有上述任何限制,除了在撰写本文时仍然是 RC(所以使用 RC 或等待它于 2018 年 12 月发布):
    • 选项 10. $x = $array[array_key_last($array)];(自 PHP 7.3 起)

有点取决于将数组用作堆栈还是队列,您可以对选项 9 进行更改.>

Ok,

I know all about array_pop(), but that deletes the last element. What's the best way to get the last element of an array without deleting it?

EDIT: Here's a bonus:

$array = array('a' => 'a', 'b' => 'b', 'c' => 'c');

or even

$array = array('a', 'b', 'c', 'd');
unset($array[2]);
echo $array[sizeof($array) - 1]; // Output: PHP Notice:  Undefined offset:  2 in - on line 4

解决方案

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 (expected Dec 13 2018).

The options (<<option code>>s) I will test are:

(functions mentioned: array_key_last , array_keys , array_pop , array_slice , array_values , count , end , reset)

The test inputs (<<input code>>s) to combine with:

  • null = $array = null;
  • empty = $array = [];
  • last_null = $array = ["a","b","c",null];
  • auto_idx = $array = ["a","b","c","d"];
  • shuffle = $array = []; $array[1] = "a"; $array[2] = "b"; $array[0] = "c";
  • 100 = $array = []; for($i=0;$i<100;$i++) { $array[] = $i; }
  • 100000 = $array = []; for($i=0;$i<100000;$i++) { $array[] = $i; }

For testing I will use the 5.6.38, 7.2.10 and 7.3.0RC1 PHP docker containers like:

sudo docker run -it --rm php:5.6.38-cli-stretch php -r '<<<CODE HERE>>>'

Each combination of the above listed <<option code>>s and <<input code>>s will be run on all versions of PHP. For each test run the following code snippet is used:

<<input code>>  error_reporting(E_ALL);  <<option code>>  error_reporting(0); $before=microtime(TRUE); for($i=0;$i<100;$i++){echo ".";for($j=0;$j<100;$j++){  <<option code>>  }}; $after=microtime(TRUE); echo "\n"; var_dump($x); echo round(($after-$before)/(100*100)*1000*1000*1000);

For each run this will var_dump the last retrieved last value of the test input and print the average duration of one iteration in femtoseconds (0.000000000000001th of a second).

The results are as follows:

/==========================================================================================================================================================================================================================================================================================================================================================================================================================\
||                                                                      ||                            T  E  S  T     I  N  P  U  T     -     5  .  6  .  3  8                            ||                             T  E  S  T     I  N  P  U  T     -     7  .  2  .  1  0                           ||                             T  E  S  T     I  N  P  U  T     -     7  .  3  .  0  R  C  1                     ||
||                                                                      ||          null |         empty |     last_null |      auto_idx |       shuffle |           100 |        100000 ||          null |         empty |     last_null |      auto_idx |       shuffle |           100 |        100000 ||          null |         empty |     last_null |      auto_idx |       shuffle |           100 |        100000 ||
||============================OPTIONS - ERRORS==========================++===============+===============+===============+===============+===============+===============+===============++===============+===============+===============+===============+===============+===============+===============++===============+===============+===============+===============+===============+===============+===============<|
||  1.  $x = array_values(array_slice($array, -1))[0];                  ||       W1 + W2 |            N1 |             - |             - |             - |             - |             - ||       W1 + W2 |            N1 |             - |             - |             - |             - |             - ||       W1 + W2 |            N1 |             - |             - |             - |             - |             - ||
||  2.  $x = array_slice($array, -1)[0];                                ||            W1 |            N1 |             - |             - |             - |             - |             - ||            W1 |            N1 |             - |             - |             - |             - |             - ||            W1 |            N1 |             - |             - |             - |             - |             - ||
||  3.  $x = array_pop((array_slice($array, -1)));                      ||       W1 + W3 |             - |             - |             - |             - |             - |             - ||  W1 + N2 + W3 |            N2 |            N2 |            N2 |            N2 |            N2 |            N2 ||  W1 + N2 + W3 |            N2 |            N2 |            N2 |            N2 |            N2 |            N2 ||
||  4.  $x = array_pop((array_slice($array, -1, 1)));                   ||       W1 + W3 |             - |             - |             - |             - |             - |             - ||  W1 + N2 + W3 |            N2 |            N2 |            N2 |            N2 |            N2 |            N2 ||  W1 + N2 + W3 |            N2 |            N2 |            N2 |            N2 |            N2 |            N2 ||
||  5.  $x = end($array); reset($array);                                ||       W4 + W5 |             - |             - |             - |             - |             - |             - ||       W4 + W5 |            N2 |            N2 |            N2 |            N2 |            N2 |            N2 ||       W4 + W5 |             - |             - |             - |             - |             - |             - ||
||  6.  $x = end((array_values($array)));                               ||       W2 + W4 |             - |             - |             - |             - |             - |             - ||  W2 + N2 + W4 |             - |             - |             - |             - |             - |             - ||  W2 + N2 + W4 |            N2 |            N2 |            N2 |            N2 |            N2 |            N2 ||
||  7.  $x = $array[count($array)-1];                                   ||             - |            N3 |             - |             - |             - |             - |             - ||            W7 |            N3 |             - |             - |             - |             - |             - ||            W7 |            N3 |             - |             - |             - |             - |             - ||
||  8.  $keys = array_keys($array); $x = $array[$keys[count($keys)-1]]; ||            W6 |       N3 + N4 |             - |             - |             - |             - |             - ||       W6 + W7 |       N3 + N4 |             - |             - |             - |             - |             - ||       W6 + W7 |       N3 + N4 |             - |             - |             - |             - |             - ||
||  9.  $x = $array[] = array_pop($array);                              ||            W3 |             - |             - |             - |             - |             - |             - ||            W3 |             - |             - |             - |             - |             - |             - ||            W3 |             - |             - |             - |             - |             - |             - ||
|| 10.  $x = $array[array_key_last($array)];                            ||            F1 |            F1 |            F1 |            F1 |            F1 |            F1 |            F1 ||            F2 |            F2 |            F2 |            F2 |            F2 |            F2 |            F2 ||            W8 |            N4 |            F2 |            F2 |            F2 |            F2 |            F2 ||
||========================OPTIONS - VALUE RETRIEVED=====================++===============+===============+===============+===============+===============+===============+===============++===============+===============+===============+===============+===============+===============+===============++===============+===============+===============+===============+===============+===============+===============<|
||  1.  $x = array_values(array_slice($array, -1))[0];                  ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||
||  2.  $x = array_slice($array, -1)[0];                                ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||
||  3.  $x = array_pop((array_slice($array, -1)));                      ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||
||  4.  $x = array_pop((array_slice($array, -1, 1)));                   ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||
||  5.  $x = end($array); reset($array);                                ||          NULL |   bool(false) |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||          NULL |   bool(false) |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||          NULL |   bool(false) |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||
||  6.  $x = end((array_values($array)));                               ||          NULL |   bool(false) |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||          NULL |   bool(false) |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||          NULL |   bool(false) |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||
||  7.  $x = $array[count($array)-1];                                   ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "b" |       int(99) |    int(99999) ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "b" |       int(99) |    int(99999) ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "b" |       int(99) |    int(99999) ||
||  8.  $keys = array_keys($array); $x = $array[$keys[count($keys)-1]]; ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||
||  9.  $x = $array[] = array_pop($array);                              ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||          NULL |          NULL |          NULL | string(1) "d" | string(1) "c" |       int(99) |    int(99999) ||
|| 10.  $x = $array[array_key_last($array)];                            ||           N/A |           N/A |           N/A |           N/A |           N/A |           N/A |           N/A ||           N/A |           N/A |           N/A |           N/A |           N/A |           N/A |           N/A ||           N/A |           N/A |           N/A |           N/A |           N/A |           N/A |           N/A ||
||=================OPTIONS - FEMTOSECONDS PER ITERATION=================++===============+===============+===============+===============+===============+===============+===============++===============+===============+===============+===============+===============+===============+===============++===============+===============+===============+===============+===============+===============+===============<|
||  1.  $x = array_values(array_slice($array, -1))[0];                  ||           803 |           466 |           390 |           384 |           373 |           764 |     1.046.642 ||           691 |           252 |           101 |           128 |            93 |           170 |        89.028 ||           695 |           235 |            90 |            97 |            95 |           188 |        87.991 ||
||  2.  $x = array_slice($array, -1)[0];                                ||           414 |           349 |           252 |           248 |           246 |           604 |     1.038.074 ||           373 |           249 |            85 |            91 |            90 |           164 |        90.750 ||           367 |           224 |            78 |            85 |            80 |           155 |        86.141 ||
||  3.  $x = array_pop((array_slice($array, -1)));                      ||           724 |           228 |           323 |           318 |           350 |           673 |     1.042.263 ||           988 |           285 |           309 |           317 |           331 |           401 |        88.363 ||           877 |           266 |           298 |           300 |           326 |           403 |        87.279 ||
||  4.  $x = array_pop((array_slice($array, -1, 1)));                   ||           734 |           266 |           358 |           356 |           349 |           699 |     1.050.101 ||           887 |           288 |           316 |           322 |           314 |           408 |        88.402 ||           935 |           268 |           335 |           315 |           313 |           403 |        86.445 ||
||  5.  $x = end($array); reset($array);                                ||           715 |           186 |           185 |           180 |           176 |           185 |           172 ||           674 |            73 |            69 |            70 |            66 |            65 |            70 ||           693 |            65 |            85 |            74 |            68 |            70 |            69 ||
||  6.  $x = end((array_values($array)));                               ||           877 |           205 |           320 |           337 |           304 |         2.901 |     7.921.860 ||           948 |           300 |           336 |           308 |           309 |           509 |    29.696.951 ||           946 |           262 |           301 |           309 |           302 |           499 |    29.234.928 ||
||  7.  $x = $array[count($array)-1];                                   ||           123 |           300 |           137 |           139 |           143 |           140 |           144 ||           312 |           218 |            48 |            53 |            45 |            47 |            51 ||           296 |           217 |            46 |            44 |            53 |            53 |            55 ||
||  8.  $keys = array_keys($array); $x = $array[$keys[count($keys)-1]]; ||           494 |           593 |           418 |           435 |           399 |         3.873 |    12.199.450 ||           665 |           407 |           103 |           109 |           114 |           431 |    30.053.730 ||           647 |           445 |            91 |            95 |            96 |           419 |    30.718.586 ||
||  9.  $x = $array[] = array_pop($array);                              ||           186 |           178 |           175 |           188 |           180 |           181 |           186 ||            83 |            78 |            75 |            71 |            74 |            69 |            83 ||            71 |            64 |            70 |            64 |            68 |            69 |            81 ||
|| 10.  $x = $array[array_key_last($array)];                            ||           N/A |           N/A |           N/A |           N/A |           N/A |           N/A |           N/A ||           N/A |           N/A |           N/A |           N/A |           N/A |           N/A |           N/A ||           370 |           223 |            49 |            52 |            61 |            57 |            52 ||
 \=========================================================================================================================================================================================================================================================================================================================================================================================================================/ 

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:
    • option .6. $x = end((array_values($array)));
    • option .8. $keys = array_keys($array); $x = $array[$keys[count($keys)-1]];
  • these options scale best for very large arrays:
    • option .5. $x = end($array); reset($array);
    • option .7. $x = $array[count($array)-1];
    • option .9. $x = $array[] = array_pop($array);
    • option 10. $x = $array[array_key_last($array)]; (since PHP 7.3)
  • 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):
    • option 10. $x = $array[array_key_last($array)]; (since PHP 7.3)

A bit depending on whether using the array as stack or as queue you can make variations on option 9.

这篇关于获取数组的最后一个元素而不删除它的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆