PHP array_unshift() Function

PHP array_unshift() Function

The array_unshift () function in PHP prepends one or more elements to the beginning or front of an array. It returns the new number of elements in the array.

Syntax

array_unshift ( array &$array [, mixed $... ] )

Parameter

array(required)- This array represents the input array.

… (optional)- This parameter represents the values to prepend.

Return

This function returns the new number of elements in the array.

Example 1

 

Output

Original Array:
 Array
 (
     [0] => Orange
     [1] => Banana
 )
 After array_unshift() function: 
 New array: 
 Array
 (
     [0] => Apple
     [1] => raspberryMango
     [2] => Orange
     [3] => Banana
 ) 

Example 2

 

Output

Original Array:
 Array
 (
     [0] => c
     [1] => Python
 ) 
 After array_unshift() function: 
 New array:
 Array
 (
     [0] => Array
         (
             [0] => PHP
             [1] => Java
             [2] => C#
         ) 
     [1] => c
     [2] => Python
 ) 

Example 3

 

Output

Original Array:
 Array
 (
     [0] => c
     [1] => Python
 )
 After array_unshift() function: 
 New array:
 Array 
 (
     [0] => Array
         (
             [0] => PHP
             [1] => Java
             [2] => C#
         )
     [1] => Array 
         (
             [0] => 1
             [1] => 2
             [2] => 3
         )
     [2] => c
     [3] => Python
 )