Javatpoint Logo
Javatpoint Logo

PHP array_unshift() function

The array_unshift( ) function is an inbuilt function of PHP. It is used to add one or more elements to the beginning of an array. This function was introduced in PHP 4.0.

Syntax

Parameters

Parameter Description Is compulsory
array The input array. compulsory
List of values The values to prepend. compulsory

Return Values

The array_unshift( ) function returns the new number of elements in the array.

Important note:

All numerical array keys will be modified to start counting from zero while literal keys won't be changed.

Example 1

Output:

1 2 3 java t point

Example 2

Output:

Array
(
    [0] =>sachin
    [1] =>sehwag
    [2] =>harbhajan
    [3] =>virat
    [4] =>rohit
    [5] =>pandeya
)

Example 3

Output:

Array
(
    [0] =>leneovo
    [1] =>acer
    [2] => dell
    [3] =>hp)

Example 4

Output:

Array
(
    [0] =>pune
    [1] =>kolkata
    [2] =>bangalore
    [3] =>delhi
)





Help Others, Please Share

facebook twitter pinterest