Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pop first element of array instead of last (reversed array_pop)?

Tags:

arrays

php

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.

like image 789
Chris Avatar asked Feb 26 '09 08:02

Chris


People also ask

What is the opposite of array pop?

array_pop() pops an element off end of the array. array_push() pushes an element into the end of the array.

What is array pop in PHP?

array_pop() pops and returns the value of the last element of array , shortening the array by one element. Note: This function will reset() the array pointer of the input array after use.

How do you select the first element of an array?

There are several methods to get the first element of an array in PHP. Some of the methods are using foreach loop, reset function, array_slice function, array_values, array_reverse, and many more. We will discuss the different ways to access the first element of an array sequentially.

Which function removes the first element of an array?

shift() The shift() method removes the first element from an array and returns that removed element. This method changes the length of the array.


2 Answers

You are looking for array_shift().

PHP Array Shift

like image 90
bojo Avatar answered Oct 08 '22 21:10

bojo


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 )  
like image 34
dreftymac Avatar answered Oct 08 '22 20:10

dreftymac