* {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:
- Returns the value of the last element in a matrix.
- Shortens the array by one element.
- Resets the array cursor for the specified array.
Here is an example of array_pop()
function.
<?php $people = ["Adam", "Andrew", "Monty", "Sajal"]; while(count($people)) { echo array_pop($people)." "; } // Output: Sajal Monty Andrew Adam ?>
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.
<?php $deliveries = ["Adam" => "LED TV", "Andrew" => "Drone", "Monty" => "Smart watch", "Sajal" => "Laptop"]; while(count($deliveries)) { end($deliveries); $person = key($deliveries); $item = array_pop($deliveries); // call delivered($item, $person); echo "Delivered ".$item." to ".$person.".\n"; } /* Delivered Laptop to Sajal. Delivered Smart watch to Monty. Delivered Drone to Andrew. Delivered LED TV to Adam. */ ?>
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:
- Returns the first element of the array.
- Shortens the array by one element.
- Numeric keys are indexed again, while letter keys remain untouched.
- 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.
<?php $people = ["Adam", "Andrew", "Monty", "Sajal"]; var_dump($people); /* array(4) { [0]=> string(4) "Adam" [1]=> string(6) "Andrew" [2]=> string(5) "Monty" [3]=> string(5) "Sajal" } */ while(count($people)) { echo array_shift($people)."\n"; if(count($people) == 2) { var_dump($people); } } /* Adam Andrew array(2) { [0]=> string(5) "Monty" [1]=> string(5) "Sajal" } Monty Sajal */ ?>
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.
<?php $orders = ["Adam" => "Pizza", "Andrew" => "Coffee", "Monty" => "Taco", "Sajal" => "Lemonade"]; while(count($orders)) { $person = key($orders); $food = array_shift($orders); // call served($food, $person); echo "Served ".$food." to ".$person.".\n"; } /* Served Pizza to Adam. Served Coffee to Andrew. Served Taco to Monty. Served Lemonade to Sajal. */ ?>
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.
