Which function does the opposite function of array_pop ()?

array_pop() returns the last value of the array and removes this value from the array.

What is the function that performs the opposite function of array_pop() , i.e. returns and deletes the first value of an array?

+6
source share
2 answers

From the php manual: -

array_shift () shifts the first value of the array and returns it, reducing the array by one element and moving everything down. All the digital keys of the array will be changed to start counting from scratch until the literal keys are affected.

I know this is a trivial question, but I searched Google for the opposite of array_pop () and nothing came up, so I decided to share this question here, and it can save a lot of people about 4 seconds each :)

+4
source

A bit more.

Functions

  • array_pop() pops element end of array

  • array_push() clicks an element into the end array.

  • array_shift() pops the element at the beginning of the array.

  • array_unshift() pushes the item to the beginning array.

Beautiful matrix

Here is a beautiful matrix that shows each function in relation to the rest. Pay attention to the symbolic arrows.

With end
Pop from array_pop() →
Click on → array_unshift() <tbsp; array_push() <-

It was very difficult to create it with the limitations of SO markup. Click on edit to check the source (and, of course, feel free to improve it)!

Shift VS. Unshift confusion

So. When trying to remember all 4 functions, it was very easy for me to find out what array_pop() doing: it just array_pop() an element from the end of the array. Naturally, the functionality of the additional array_push() was absolutely clear from the first shot: it returns the element back to the end. However, the two opposite brothers array_shift() and array_unshift() continued to give me a hard time when I was trying to remember which one appears and who pushes.: // I had to constantly look for it.

Funny, the solution I came up with was just the letter ā€œUā€ . Those who have this: array_push() and array_unshift() are pUsh. The other two are pop. Easy-Peasy!

Hope this helps anyone!

+14
source

Source: https://habr.com/ru/post/919991/


All Articles