Insert element "blue" into the array:
<?php$a=array("a"=>"red","b"=>"green");array_unshift($a,"blue");print_r($a);?>The array_unshift() function is used to insert new elements into an array. The values of the new array will be inserted at the beginning of the array.
Tip: You can insert one or more values.
Note: Numeric key names will start at 0 and increase by 1. String key names will remain unchanged.
array_unshift( array,value1,value2,value3... )
parameter | describe |
---|---|
array | Required. Specifies an 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. |
Return value: | Returns the number of elements in the new array. |
---|---|
PHP version: | 4+ |
Show the return value:
<?php$a=array("a"=>"red","b"=>"green");print_r(array_unshift($a,"blue"));?>Using numeric keys:
<?php$a=array(0=>"red",1=>"green");array_unshift($a,"blue");print_r($a);?>