php 弹出数组的第一个元素而不是最后一个(反转 array_pop)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/589601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pop first element of array instead of last (reversed array_pop)?
提问by Chris
Is there a PHP function that would 'pop' first element of array? array_pop()pops last element, but I'd like to pop the first.
是否有一个 PHP 函数可以“弹出”数组的第一个元素?array_pop()弹出最后一个元素,但我想弹出第一个。
回答by bojo
回答by dreftymac
Quick CheatsheetIf you are not familiar with the lingo, here is a quick translation of alternate terms, which may be easier to remember:
快速备忘单如果您不熟悉术语,这里是替代术语的快速翻译,可能更容易记住:
* array_unshift() - (aka Prepend ;; InsertBefore ;; InsertAtBegin )
* array_shift() - (aka UnPrepend ;; RemoveBefore ;; RemoveFromBegin )
* array_push() - (aka Append ;; InsertAfter ;; InsertAtEnd )
* array_pop() - (aka UnAppend ;; RemoveAfter ;; RemoveFromEnd )
回答by Makame
For me array_slice() works fine, Say:
对我来说 array_slice() 工作正常,说:
$arr = array(1,2,3,4,5,6);
//array_slice($array,offset); where $array is the array to be sliced, and offset is the number of array elements to be sliced
$arr2 = array_slice($arr,3);//or
$arr2 = array_slice($arr,-3);//or
$arr2 = array_slice($arr,-3,3);//return 4,5 and 6 in a new array
$arr2 = array_slice($arr,0,4);//returns 1,2,3 and 4 in a new array
回答by roninpawn
While array_shift()is definitely the answer to your question, it's worth mentioning that it can be an unnecessarily slow process when working with large arrays. (or many iterations)
虽然array_shift()绝对是您问题的答案,但值得一提的是,在处理大型阵列时,这可能是一个不必要的缓慢过程。(或多次迭代)
After retrieving the first element, array_shift()re-indexes all numerical keys in an array, so that the element that used to be at [1]becomes [0]and an element at [10000]becomes [9999].
检索所述第一元件之后,array_shift()重新索引的所有数字键在阵列中,从而使曾经是在元件[1]变得[0]和在元件[10000]变得[9999]。
The larger the array, the more keys to re-index. And if you're iterating over large arrays, calling array_shifton multiple iterations, the performance hit can rack up.
数组越大,重新索引的键就越多。如果您在大型数组array_shift上进行迭代,调用多次迭代,则性能损失可能会增加。
Benchmarks show for large arrays it's often cheaper to reverse the array order and pop the elements from the end.
基准测试表明,对于大型数组,颠倒数组顺序并从末尾弹出元素通常更便宜。
$array = array_reverse($array);
$value = array_pop($array);
array_pop()obviously doesn't need to re-index since it's taking from the end, and array_reverse()returns a new array by simply copying the array passed to it, from back to front. array_reverse($array, true)can be used to preserve key => valuepairs where needed.
array_pop()显然不需要重新索引,因为它从末尾开始,并array_reverse()通过简单地从后到前复制传递给它的数组来返回一个新数组。array_reverse($array, true)可用于key => value在需要时保留对。
[EDIT] I should mention, the downside to the reverse-pop method is memory consumption, where array_reverse()generates a new array, while array_shift()works in place. Thus takes longer.
[编辑] 我应该提到,reverse-pop 方法的缺点是内存消耗,在那里array_reverse()生成一个新数组,同时array_shift()工作到位。因此需要更长的时间。

