Tasty In-Out Data

11 03 2011

Pieces of fruit in a basket

Image via Wikipedia

I had occasion today to revisit the subject of stacks and queues. These are both data structures and until PHP had the Standard PHP Library (SPL), the only functionality that PHP offered for you to manipulate data as a stack or queue, was two pairs of functions, array_push and array_pop for stacks, and array_unshift and array_shift for queues.

If you wanted to retrieve data on a First-In-First-Out (FIFO) basis, namely first element in, first element out, then you’d need to use array_shift. If you think of a queue as a line, then it makes sense that the first item in line should merit attention!

On the other hand if you needed to obtain data on a Last-In-First-Out (LIFO) basis then array_pop() was the function to use, since it would take off first the last item that was added to the array, where in this situation, it is being treated as if it were a stack.

The above functions are the old-fashioned way of doing things in PHP. To see how they work let’s consider an array of fruit containing apples, oranges, bananas, grapes, and melons. Let’s add some cherries, treating the array as if it were a queue, and then remove an element to demonstrate how FIFO works with the relevant functions.

$a = array("apples","oranges","bananas","grapes","melons");
array_unshift($a,"cherries"); // Adds element(s) to start of queue
foreach ($a as $fruit) {
    echo $fruit . " ";
echo nl2br("\nremoving 'first in' which is ...\n");
$out = array_shift($a);// apples sh/b out
echo nl2br("\n$out\n");

Manipulating the array as if it were a stack, i.e. think of the array elements stacked one on top of the other with the very first one on the bottom since stacks are characterized by LIFO. Here’s an example in which I push some pears on top of our original fruit array:

$a = array("apples","oranges","bananas","grapes","melons");
array_push($a,"pears");  // pears sh/b on top of stack
echo nl2br("\nthe array now ...");
var_dump( array_reverse($a) ); // to properly view stack!
echo nl2br("\npopping off last element first ...\n");
$out = array_pop($a);  // pears sh/b out.
echo nl2br("$out\n");

The SPL is starting to become better documented so you might want to refer to it when it comes to creating stacks or queues. In fact there might be a performance boost if you use the SPL with respect to creating the aforementioned data containers.

The following snippets are a result of my trying out these new features in PHP5’s SPL.

function foo()
    $queue = new SplQueue();
    $args = func_get_args();
	$max = func_num_args();
    for ($i=0; $i < $max; $i++) {  
	     $queue->enqueue( $args[ $i]);    // pushing element to end of queue
    echo nl2br("\nElements in queue are ") . count($queue). "<br>";
    echo nl2br("\nThe first is: ") . $queue[0];
    echo nl2br("\nNow removing one element\n");
    $queue->dequeue(); // should loose apples
    return $queue;

function bar()
     $stack = new SplStack();
     $args = func_get_args();
	 $max = func_num_args();
     for ($i=0; $i < $max; $i++) {
	     $stack->push($args[ $i]);
     return $stack;

$foo = foo("apples","pears","bananas","peaches");
$bar = bar("red","white","blue","pink");

echo nl2br("\nwhat is in the queue:\n");
foreach($foo as $f) { 
   echo $f . "<br />";
echo nl2br("\n\nwhat is in the stack:\n");
foreach($bar as $b){
   echo $b . "<br />";

The great thing about our stack and queue objects is that we can iterate through them just as if they were arrays using a foreach loop which is due to the particular design of these objects; see the online manual for the interfaces the SplQueue and SplStack implement.

Useful links:



This work is licensed under a Creative Commons License



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: