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:
- Returns the value of the last element of an array.
- Shortens the array by one element.
- Resets the array pointer of the specified array.
Here is an example of the 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()
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.
<?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 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:
- Returns the first element of the array.
- Shortens the array by one element.
- Numerical keys are re-indexed, while literal keys are left untouched.
- 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.
<?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’s no need to change the internal array pointer because array_shift()
does 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. */ ?>
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.