array_unshift
Insert one or more units at the beginning of an array
array_unshift()
function is used to insert new elements into an array. The value of the new array will be inserted at the beginning of the array.
The added elements are added as a whole, the order in the array is the same as in the parameters.
This function returns the number of elements in the array.
Tip: You can insert one or more values.
Note: The numeric key names will start at 0 and increment by 1. The string key name will remain unchanged.
Insert the element "blue" into the array:
<?php $a = array ( "a" => "red" , "b" => "green" ) ; array_unshift ( $a , "blue" ) ; print_r ( $a ) ; ?>
Try it yourself
Show return value:
<?php $a = array ( "a" => "red" , "b" => "green" ) ; print_r ( array_unshift ( $a , "blue" ) ) ; ?>
Try it yourself
Use the numeric key name:
<?php $a = array ( 0 => "red" , 1 => "green" ) ; array_unshift ( $a , "blue" ) ; print_r ( $a ) ; ?>
Try it yourself
array_unshift ( array , value1 , value2 , value3 ... )
parameter | describe |
---|---|
array | Required. Specify array. |
value1 | Required. Specifies the value to be inserted. |
value2 | Optional. Specifies the value to be inserted. |
value3 | Optional. Specifies the value to be inserted. |