PHP » Arrays » array_shift()

Syntax:
mixed array_shift(array array)
array
The array to perform the operation on.

Removes and returns an element from the beginning of the array.

array_shift() removes an element from the beginning of an array. The removed element is returned. array_shift() is similar to array_pop() in usage, but pops an element off the beginning of the array, rather than the end.

Examples

Code:
<?php

$array1 = array(1);
array_unshift($array1, 2, 3, 4);
array_unshift($array1, 5);
array_unshift($array1, 6);
array_unshift($array1, 7);

for ($i = 0; $i < 7; $i++) {
   print array_shift($array1) . " ";
}

?>
Output:
7 6 5 2 3 4 1
Explanation:

Unshifts some elements into an array, and shifts them out again. Note the ordering of the output.

See Also: