PHP 8 reference · Array Functions

PHP array_unshift() Function

Add one or more elements to the start of an array and return its new length. Numeric keys are renumbered.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

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

Parameters

ParameterDescription
array The array (changed in place).
values The values to prepend, kept in this order.

Return value

The new number of elements.

Examples

Prepend valuesTry it yourself
<?php
$list = ["c", "d"];
array_unshift($list, "a", "b");
print_r($list);

Output

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)
Add a first breadcrumbTry it yourself
<?php
$crumbs = ["Docs", "Arrays"];
array_unshift($crumbs, "Home");
echo implode(" > ", $crumbs), "\n";

Output

Home > Docs > Arrays

Version history

VersionChange
PHP 4.0Added
PHP 7.3Can be called with only the array argument.

In other languages

Was this page helpful?