163 of 410 menu

The array_unshift Function

The array_unshift function adds elements to the beginning of an array. The passed array is modified, and the function returns the new number of elements in the array. Elements to be added are listed separated by commas.

Syntax

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

Example

Let's add 2 more elements to the beginning of the array:

<?php $arr = [1, 2, 3, 4, 5]; $num = array_unshift($arr, 'a', 'b'); var_dump($arr); ?>

Code execution result:

['a', 'b', 1, 2, 3, 4, 5]

The $num variable will contain the new number of array elements:

7

See Also

  • the array_shift function,
    which extracts the first element of an array
byenru