Hudzilla.org - the homepage of Paul Hudson
Contents > Arrays > Array-specific functions Wish List | Report Bug | About Me ]

5.6.6     Using an array as a double-ended queue: array_shift(), array_unshift(), array_push(), array_pop()

This is NOT the latest copy of this book; click here for the latest version.

mixed array_shift ( array array)

int array_unshift ( array array, mixed var [, mixed ...])

int array_push ( array array, mixed var [, mix ...])

mixed array_pop ( array array)

If you have ever used the C++ Standard Template Library, you will know how useful the deque (double-ended queue, pronounced "deck") type is. If you have never used STL, or C++ for that matter, a deque allows you to store elements in an array-like structure, then work with either the very first element or the very last element. This might not sound useful, after all we've already seen that PHP lets you read from any variable in an array at any time, so why would it be good to work with only the first and last elements?

Well, using what you know already, how would you insert a variable at the very first position in your array? Or how would you read the first element and remove it from the array? It would be tricky, for sure - and that's where the deque principle comes in.

Array_shift() takes an array as its only parameter, and returns the value from the front of the array while also removing it from the array. Consider this script:

<?php
    $names
= array("Johnny", "Timmy", "Bobby", "Sam", "Tammy", "Danny", "Joe");
    
$firstname = array_shift($names);
    
var_dump($names);
?>

If you run that script, you will see that $firstname is set to "Johnny", and the array $names now only has the values Timmy, Bobby, Sam, Tammy, Danny, and Joe - Johnny gets removed. To remove an element from the end of an array (as opposed to the start), there is also array_pop(), which works in the same way as array_shift(). Both these functions also have opposites, array_unshift() and array_push(), which place an element into an array at the start or at the end respectively. This next script should clear up any confusion:

<?php
    $names
= array("Johnny", "Timmy", "Bobby", "Sam", "Tammy", "Danny", "Joe");
    
$firstname = array_shift($names);
    
// First element of $names is now Timmy; last is Joe

    
array_push($names, $firstname);
    
// first is Timmy; last is now Johnny

    
$firstname = array_pop($names);
    
// first is Timmy; last is Joe again

    
array_unshift($names, $firstname);
    
// first is Johnny, last is Joe

    
var_dump($names);
?>

Extracting the first and last variable from an array is a very popular task, so try to keep these functions in the back of your mind.





<< 5.6.5 Checking whether an element exists: in_array()   5.6.7 Swapping keys and values: array_flip() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus six?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow