Pop and Shift Arrays With PHP: When to Use Each One

* { box-sizing: border-box; } body {margin: 0;}*{box-sizing:border-box;}body{margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;}

PHP has a lot of built-in array functions to help with many common tasks. The presence of so many functions, though, can also be a bit overwhelming because sometimes you have to keep the slight differences between these functions in mind.

It is also possible to use different combinations of a few functions to achieve the same thing. This means that having a clear understanding of these functions will help you write better performing code in fewer lines.

In this post, you’ll learn about the array_pop() and array_shift() functions in PHP.

Pop Arrays With PHP

There is a dedicated function called array_pop() to help you pop an element off the end of a given array in PHP. It does three things:

  1. Returns the value of the last element of an array.
  2. Shortens the array by one element.
  3. Resets the array pointer of the specified array.

Here is an example of the array_pop() function.

You can use array_pop() in combination with end() and key() in order to get the last key-value pair from an array. Here’s a hypothetical example where we have to make a bunch of deliveries to different people.

We use end() to move the array pointer to the last element. The key() function helps us get the name of the person, and array_pop() gets the name of the item while removing it from the array.

The array_pop() function is useful when you want to implement a LIFO or Last In First Out system. You should consider using it when you are dealing with a bunch of elements and want to access the last element while removing it from the array at the same time.

Shift Arrays With PHP

The array_shift() function will shift an element off the start of an array. It does the following to your array:

  1. Returns the first element of the array.
  2. Shortens the array by one element.
  3. Numerical keys are re-indexed, while literal keys are left untouched.
  4. Resets the array pointer of the specified array.

Here’s an example of the array_shift() function. You can see that the numerical keys have been re-indexed by starting the count from zero.

You can get key-value pairs out of an associative array in PHP by using a combination of key() and array_shift(). There’s no need to change the internal array pointer because array_shift() does it automatically for you.

The array_shift() function is useful when you want to implement a FIFO or First In First Out system. In our example above, we simulated a system where food orders are served to customers in a restaurant based on who ordered first.

Final Thoughts

In this tutorial, you learned how to get the last or first element of an array in PHP using the array_pop() and array_shift() functions in PHP. You can also use end() and key() with these functions to get key-value pairs from an associative array. The performance of array_shift() can be slow when dealing with very large arrays. You might want to use array_reverse() and then array_pop() in that case.

Leave a comment

Your email address will not be published.