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 features to help with many common tasks. However, the presence of so many features can also be a bit overwhelming because sometimes you have to keep the small differences between these features in mind.

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

In this post you will learn about array_pop() and array_shift() features in PHP.

Pop Arrays with PHP

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

  1. Returns the value of the last element in a matrix.
  2. Shortens the array by one element.
  3. Resets the array cursor for the specified array.

Here is an example of array_pop() function.

You can use array_pop() in combination with end() and key() to get the last key-value pair from a matrix. Here is a hypothetical example where we have to make a lot of deliveries to different people.

We use end() to move the array cursor to the last item. That key() function helps us to get the name of the person, and array_pop() retrieves the name of the item while removing it from the array.

That array_pop() this feature 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 lot of items and want to access the last item while removing it from the array at the same time.

Change arrays with PHP

That array_shift() function will move an element from the beginning of an array. It does the following with your array:

  1. Returns the first element of the array.
  2. Shortens the array by one element.
  3. Numeric keys are indexed again, while letter keys remain untouched.
  4. Resets the array cursor for the specified array.

Here is an example of array_shift() function. You can see that the numeric 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 is no need to change the internal array cursor because array_shift() do it automatically for you.

That array_shift() this feature 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.

Concluding thoughts

In this tutorial you learned how to get the last or first element of an array in PHP using array_pop() and array_shift() features 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 may want to use array_reverse() also array_pop() in that case.

Leave a Comment