The built-in array() data type has many ways that it can be manipulated. One of those ways is through the array_shift()
function that treats an array as a queue, taking array elements from the front of the queue and moving up all of the other items in the array.
When you define an array that uses numeric keys, all of the keys are treated as regular array indices. The key represents the physical position of the item in the array, so a key of 0 (zero) represents the element at position zero, a key of 1 (one) represents the element at position one, and so on through however many items are in the array. Using array_shift()
on this array with numeric keys will change all of the keys on each element so that they begin counting from zero. The second element, which had a key of one, now has a key of zero. The third element now has a key of two, and so on.
But…
When you use array_shift()
on an array with non-numeric keys, letters or names or objects for example, the array_shift()
function does not re-index the keys, leaving them untouched.
array_shift()
Functionmixed array_shift(array &$source_array)
source_array |
Required |
The source array that will have a value shifted from the beginning. |
Returns the first element at the beginning of the array. The first element is removed from the array and all other elements are shifted to a lower index.
Elements that use numeric keys are re-indexed from zero. Keys that are non-numerical, i.e. alpha keys are left untouched.
When the array_shift function is invoked, the internal array pointer is reset so its previous value should not be relied upon.
Specifying a non-array as the source, an array()
that is NULL
, or an empty array will return NULL
.
This example demonstrates how to treat an array as a queue with non-numeric keys by removing a single item from the beginning of the array, that is, the front of the queue, through the use of the array_shift()
function. The code demonstrates the fact that in arrays with non-numeric keys the keys are not changed, unlike an array with numeric keys which is automatically re-indexed, when an element is removed from the beginning of the array.
array_shift()
function to remove an item from the beginning of each array.<?php
// allocate a small array of integers
$numeric_keys = array('JUSTIN', 'CATHERINE', 'AMANDA', 'NICHOLAS', 'GARETH', 'CHRISTOPHER', 'PAUL', 'ROBERT', 'GAIL', 'KEVYN') ;
// allocate a small array of strings
$alpha_keys = array('A' => 'JUSTIN', 'B' => 'CATHERINE', 'C' => 'AMANDA', 'D' => 'NICHOLAS', 'E' => 'GARETH', 'F' => 'CHRISTOPHER', 'G' => 'PAUL', 'H' => 'ROBERT', 'I' => 'GAIL', 'J' => 'KEVYN') ;
// output the array of names with integer keys
echo 'Numeric keys: ' . print_r($numeric_keys, true) . '<br/><br/>';
// output the array of names with alpha keys
echo 'Alpha keys: ' . print_r($alpha_keys, true) . '<br/><br/>';
// take a single element from the beginning of each of the arrays
$numeric_item = array_shift($numeric_keys);
$alpha_item = array_shift($alpha_keys);
// display the elements that were taken
echo 'Numeric item: ' . print_r($numeric_item, true) . '<br/><br/>';
echo 'Alpha item: ' . print_r($alpha_item, true) . '<br/><br/>';
// output the modified array of names with integer keys
echo 'Numeric keys: ' . print_r($numeric_keys, true) . '<br/><br/>';
// output the modified array of names with alpha keys
echo 'Alpha keys: ' . print_r($alpha_keys, true) . '<br/><br/>';
?>
Numeric keys: Array ( [0] => JUSTIN [1] => CATHERINE [2] => AMANDA [3] => NICHOLAS [4] => GARETH [5] => CHRISTOPHER [6] => PAUL [7] => ROBERT [8] => GAIL [9] => KEVYN )
Alpha keys: Array ( [A] => JUSTIN [B] => CATHERINE [C] => AMANDA [D] => NICHOLAS [E] => GARETH [F] => CHRISTOPHER [G] => PAUL [H] => ROBERT [I] => GAIL [J] => KEVYN )
Numeric item: JUSTIN
Alpha item: JUSTIN
Numeric keys: Array ( [0] => CATHERINE [1] => AMANDA [2] => NICHOLAS [3] => GARETH [4] => CHRISTOPHER [5] => PAUL [6] => ROBERT [7] => GAIL [8] => KEVYN )
Alpha keys: Array ( [B] => CATHERINE [C] => AMANDA [D] => NICHOLAS [E] => GARETH [F] => CHRISTOPHER [G] => PAUL [H] => ROBERT [I] => GAIL [J] => KEVYN )
The program output shows the two arrays and how the position of each item in the respective array is represented. The first array, $numeric_keys
using integers, or numeric keys, translating directly to array indices, and the second, $alpha_keys
using non-numeric values, in this case in the form of a single upper-case letter.
The shift operation removes the first element from each array respectively, and are of course equal to each other. What is interesting is what happens to each array after the array_shift()
function has completed its work.
The $numeric_array
array which uses numeric keys to represent positions has been re-indexed so that the first element, which is now “CATHERINE” is at an index of zero and all other array elements continue on in a linear sequence of indices. The $alpha_keys
array, even though the first element is also “CATHERINE”, the key of that element, and all subsequent elements, remains untouched.
Using the array_shift()
function on arrays with non-numeric keys can prevent any second-hand side effects to the indexing of the array elements. However, you also have to be careful of unintended consequences because of this, expecting an array to be re-indexed when it won’t because you used non-numeric keys, or expecting an array to not be re-indexed, and it is, because you used numeric keys.