Site Search
Homepage of Otaku No Zoku
Complete Archives of Otaku No Zoku
About Otaku No Zoku
Subscribe to Otaku No Zoku
Bookmark Otaku No Zoku

PHP array_shift() Function Definition :

PHP has a built-in array() data type which can be easily treated as though it were a list or a queue data type. The way to do this is through the use of the array_shift() function which takes the element at the beginning of the array and returns it, and in the process, shortens the length of the array by one.

The queue data type sometimes referred to as first-in, first-out, unlike a stack which is last-in, first-out, feeds items in at the end of the queue, and removes them from the front of the queue in the order they entered. If you have ever had to stand in line for anything, with people in front of you, and people behind you, waiting your turn until you are served, you know what a queue is.

PHP array_shift() Function

mixed array_shift(array &$source_array)

source_array

Required

The source array that will have a value shifted from the beginning.

Returns the first element of the array, removing it from the array and shifting all other elements down by one.

Elements that have a numerical position within the array will be re-indexed so that their position numbers begin at zero. Non-numerical keys will remain untouched.

PHP maintains an array pointer for each array used in iteration, and other array operations. Invoking this function will reset the array pointer.

Invoking this function when the argument is not an array() data type or an empty array will return NULL.

Pulling Items Out Of Your Queue

This example demonstrates how to take and element from the front of the queue by treating a PHP array as a queue data structure.

Steps

  1. Allocate a small array of integers.
  2. Print the original array of integers and the position in the array each integer exists at.
  3. Use the array_shift() function to remove an item from the beginning of the array.
  4. Print the value of the of the item removed, i.e. the first integer in the array.
  5. Print the now shorter array of integers.

The Source Code

<?php
// allocate a small array of integers
$queue = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

// output the array of integers before it is manipulated
print_r($queue);
	
// take a single element from the beginning of the array
// that is, pull items from the front of the queue
$item = array_shift($queue);
	
// output the value of the item taken
print_r($item);

// output each remaining number in the array
print_r($queue);
?>

Program Output

Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 )

1

Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 [4] => 6 [5] => 7 [6] => 8 [7] => 9 [8] => 10 ) 

Results

As you can see the output of the program shows that the array begins life with ten elements, in this case integers from 1 through 10. The array is then manipulated with array_shift(), the element at the start of the array is returned, and displayed, and the now modified array is displayed showing that only nine elements exist. If the process were repeated enough times the array would eventually be emptied of all data and NULL would be returned indicating no more elements are available.

Summary

Because PHP arrays are so versatile and you are able to treat them as not only arrays, but dictionaries and hash maps and stacks and queues you have the ability to think more about the problem you are trying to solve and less about which data structure needs to be used in a particular scenario. For the most part, you can just stick your data in an array and be done with it.

Liked This Post?

Subscribe to the RSS feed or follow me on Twitter to stay up to date!