array_unshift

(PHP 4, PHP 5, PHP 7)

array_unshiftPrepend one or more elements to the beginning of an array

Description

array_unshift ( array &$array , mixed ...$values ) : int

array_unshift() prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won't be changed.

Parameters

array

The input array.

values

The values to prepend.

Return Values

Returns the new number of elements in the array.

Changelog

Version Description
7.3.0 This function can now be called with only one parameter. Formerly, at least two parameters have been required.

Examples

Example #1 array_unshift() example

<?php
$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");
print_r($queue);
?>

The above example will output:

Array
(
    [0] => apple
    [1] => raspberry
    [2] => orange
    [3] => banana
)

See Also

© 1997–2020 The PHP Documentation Group
Licensed under the Creative Commons Attribution License v3.0 or later.
https://www.php.net/manual/en/function.array-unshift.php