Pop and Shift Arrays With PHP: When to Use Each One
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.
1 |
<?php
|
2 |
|
3 |
$people = ["Adam", "Andrew", "Monty", "Sajal"]; |
4 |
|
5 |
while(count($people)) { |
6 |
echo array_pop($people)." "; |
7 |
}
|
8 |
// Output: Sajal Monty Andrew Adam
|
9 |
|
10 |
?>
|
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.
1 |
<?php
|
2 |
|
3 |
$deliveries = ["Adam" => "LED TV", "Andrew" => "Drone", "Monty" => "Smart watch", "Sajal" => "Laptop"]; |
4 |
|
5 |
while(count($deliveries)) { |
6 |
end($deliveries); |
7 |
$person = key($deliveries); |
8 |
$item = array_pop($deliveries); |
9 |
|
10 |
// call delivered($item, $person);
|
11 |
echo "Delivered ".$item." to ".$person.".\n"; |
12 |
}
|
13 |
|
14 |
/*
|
15 |
Delivered Laptop to Sajal.
|
16 |
Delivered Smart watch to Monty.
|
17 |
Delivered Drone to Andrew.
|
18 |
Delivered LED TV to Adam.
|
19 |
*/
|
20 |
|
21 |
?>
|
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.
1 |
<?php
|
2 |
|
3 |
$people = ["Adam", "Andrew", "Monty", "Sajal"]; |
4 |
|
5 |
var_dump($people); |
6 |
/*
|
7 |
array(4) {
|
8 |
[0]=>
|
9 |
string(4) "Adam"
|
10 |
[1]=>
|
11 |
string(6) "Andrew"
|
12 |
[2]=>
|
13 |
string(5) "Monty"
|
14 |
[3]=>
|
15 |
string(5) "Sajal"
|
16 |
}
|
17 |
*/
|
18 |
|
19 |
while(count($people)) { |
20 |
echo array_shift($people)."\n"; |
21 |
if(count($people) == 2) { |
22 |
var_dump($people); |
23 |
}
|
24 |
}
|
25 |
|
26 |
/*
|
27 |
Adam
|
28 |
Andrew
|
29 |
|
30 |
array(2) {
|
31 |
[0]=>
|
32 |
string(5) "Monty"
|
33 |
[1]=>
|
34 |
string(5) "Sajal"
|
35 |
}
|
36 |
|
37 |
Monty
|
38 |
Sajal
|
39 |
*/
|
40 |
|
41 |
|
42 |
?>
|
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.
1 |
<?php
|
2 |
|
3 |
$orders = ["Adam" => "Pizza", "Andrew" => "Coffee", "Monty" => "Taco", "Sajal" => "Lemonade"]; |
4 |
|
5 |
while(count($orders)) { |
6 |
$person = key($orders); |
7 |
$food = array_shift($orders); |
8 |
|
9 |
// call served($food, $person);
|
10 |
echo "Served ".$food." to ".$person.".\n"; |
11 |
}
|
12 |
/*
|
13 |
Served Pizza to Adam.
|
14 |
Served Coffee to Andrew.
|
15 |
Served Taco to Monty.
|
16 |
Served Lemonade to Sajal.
|
17 |
*/
|
18 |
|
19 |
?>
|
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.



