PHP » Arrays » array_unshift()

Syntax:
int array_unshift(array array, mixed element1 [, mixed element2, ...])
array
The array to which elements should be added.
element1
The new first element of the array.
element2
The new second element of the array.

Adds elements to the beginning of an array.

With array_unshift(), one or more elements can be added to the beginning of an array. This function is very similar to array_push(), but it pushes elements onto the beginning rather than the end of the array. The function returns the number of elements that were added to the array.

Examples

Code:
<?php

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

foreach ($array1 as $element) {
   print "$element ";
}

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

The new elements in the array will be ordered the same way as the parameters.

See Also: