Pop and Shift Arrays with PHP: When to Use Each



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

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

It is also possible to use different combinations of some 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 will learn about the array_pop() And array_shift() functions in PHP.

Pop Array with PHP

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

  1. Returns the value of the last element of an array.
  2. Shorten the array by an element.
  3. Resets the array pointer of 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 an array. Here is a hypothetical example where we have to make a lot of deliveries to different people.

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

The array_pop() The 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 by removing it from the array at the same time.

Move arrays with PHP

The array_shift() function will move an element to the beginning of an array. Do the following to your array:

  1. Returns the first element of the array.
  2. Shorten the array by an element.
  3. The number keys are reindexed, while the letter keys are not touched.
  4. Resets the array pointer of the specified array.

Here is an example of array_shift() function. You can see that the number keys have been reindexed starting the count from zero.

You can get key-value pairs from an associative array in PHP using a combination of key() And array_shift(). You don’t need to change the internal array pointer because array_shift() it does it automatically for you.

The array_shift() The feature is useful when you want to implement a FIFO or First In First Out system. In our example above, we have 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 file 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 may want to use array_reverse() then array_pop() in that case.



Source link

By LocalBizWebsiteDesign

Leave a Reply

Your email address will not be published. Required fields are marked *