38

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Chris
  • 3,405
  • 4
  • 29
  • 34

5 Answers5

71

You are looking for array_shift().

PHP Array Shift

bojo
  • 1,407
  • 13
  • 15
38

Quick Cheatsheet If 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 ) 
dreftymac
  • 31,404
  • 26
  • 119
  • 182
3

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)

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].

The larger the array, the more keys to re-index. And if you're iterating over large arrays, calling array_shift on multiple iterations, the performance hit can rack up.

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 => value pairs where needed.

[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.

roninpawn
  • 161
  • 1
  • 7
  • Is array_reverse() really faster than array_shift()? Array_reverse() still has to do a pass on every element during the copy. Where is the savings? And with the added memory consumption, wouldn't array_reverse() be slower than array_shift()? Did you benchmark? Also, I don't know how PHP calculates array indexing, but I assume they are not linked lists (maybe they are?). An array is a pointer to a memory block. Therefore, PHP could be implemented to free the first array element and point the array to the second element's memory address. I do not know how PHP actually implements arrays though. –  Apr 12 '20 at 00:14
  • @BlueWater Here's a link to someone else's benchmark results. http://www.evardsson.com/blog/2010/02/05/comparing-php-array_shift-to-array_pop/ It's been ages since I even thought about PHP, but you got me thinking again. Is it that flipping the head and tail values are all that's necessary in a reverse? I'm honestly not sure what array_reverse() is doing internally. But its not re-indexing the array it returns. Maybe you can tell me! I think you'd enjoy this article, if you haven't seen it: https://nikic.github.io/2012/03/28/Understanding-PHPs-internal-array-implementation.html – roninpawn Apr 13 '20 at 04:27
  • Thanks for the link. The benchmark link tests multiple passes over the array. But nowhere on this page is multiple passes mentioned. I thought you were talking about a single pop. Certainly for multiple pops, a single reverse will yield linear time, whereas shift will yield n^m time, where m is the number of pops. –  Apr 14 '20 at 05:41
2

For me array_slice() works fine, Say:

$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
Makame
  • 21
  • 2
0

Use key() and unset().

When array's keys is Numeric, array_shift() change array keys to started from 0.

$arr = [5 => 5, 71 => 71, 1 => 1, 8 => 8];
$first_key = key($arr);
$first_element = $arr[$first_key];  //       <-- get first element of array
echo $first_element;  // print '5'
unset($arr[$first_key]);  //                           <-- delete first element of array
thor son
  • 11
  • 2