PHP array_unshift() Function

The array_unshift() is used to add one or more elements to the beginning of an array.

Tip: You can add one value, or as many as you like.

Note: Numeric keys will start at 0 and increase by 1. String keys will remain the same.

ParameterDescription
arrayRequired. Specifying an array
value1Required. Specifies a value to insert
value2Optional. Specifies a value to insert
value3Optional. Specifies a value to insert

Syntax

 array_unshift(array,value1,value2,value3...) 

Example

$array = array("a"=>"Cooper","b"=>"Samuel");
array_unshift($array,"James");
print_r($array);

Output

Array ( [0] => James [a] => Cooper [b] => Samuel ) 

Leave a Reply

Your email address will not be published. Required fields are marked *