The array_unshift() function adds one or more elements to the beginning of an array.
Syntax
array_unshift(arr, val1, val2, val3)
Parameters
arr − The specified array. Required.
val1 − Value to be inserted. Required.
val2 − Value to be inserted. Optional.
val3 − Value to be inserted. Optional.
Return
The array_unshift() function returns the new elements in the array.
Example
The following is an example −
<?php $arr = array("p"=>"one","q"=>"two"); print_r(array_unshift($arr,"three")); ?>
Output
The following is the output −
3
Example
Let us see another example −
<?php $arr = array("p"=>"one","q"=>"two"); array_unshift($arr,"three"); print_r($arr); ?>
Output
The following is the output −
Array ( [0] => three [p] => one [q] => two )