array_unshift() definition and usage
array_unshift() function inserts one or more elements at the beginning of the array.
The added elements are added as a whole, and the order of these elements in the array is the same as the order in the parameters.
This function will return the number of elements in the array.
Syntax
array_unshift(array,value1,value2,value3...) Parameter Description
array Required. Specifies the input 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.
Tips and Notes
Note: All numeric key names will be modified to start counting from zero, and all string key names will remain unchanged.
Example 1
Copy code The code is as follows:
$a= array("a"=>"Cat","b"=>"Dog");
array_unshift($a,"Horse");
print_r($a);
?> ;
Output:
Array ( [0] => Horse [a] => Cat [b] => Dog ) Example 2
Return key value :
Copy code The code is as follows:
$a=array("a"=> ;"Cat","b"=>"Dog");
print_r(array_unshift($a,"Horse"));
?>
Output:
3 Example 3
Array with numeric keys:
Copy code The code is as follows:
< ;?php
$a=array(0=>"Cat",1=>"Dog");
array_unshift($a,"Horse");
print_r($a);
?>
Output:
Array ( [0] => Horse [1] => Cat [2] => Dog )
http://www.bkjia.com/PHPjc/324613.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324613.htmlTechArticlearray_unshift() definition and usage array_unshift() function inserts one or more elements at the beginning of the array. The added elements are added as a whole, and the order of these elements in the array is the same as...