Usage of array_unshift function in php: [array_unshift(array,value1)]. The array_unshift function is used to insert new elements into the array. The new elements will be inserted at the beginning of the array.
How to use the php array_unshift function?
(Recommended tutorial: php video tutorial)
Function: Used to insert new elements into the array. The values of the new array will be inserted at the beginning of the array.
Syntax:
array_unshift(array,value1,value2,value3...)
Parameters:
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.
Explanation:
The added elements are added as a whole. The order of these elements in the array is the same as the order in the parameters. This function returns the number of elements in the array. You can insert one or more values. Numeric key names will start at 0 and increase by 1. String key names will remain unchanged.
php array_unshift() function usage example 1
<?php $a=array("a"=>"西门","b"=>"灭绝"); print_r(array_unshift($a,"无忌")); echo "<br>"; print_r($a); ?>
Output:
3 Array ( [0] => 无忌 [a] => 西门 [b] => 灭绝 )
php array_unshift() function usage example 2
<?php $a=array("a"=>"欧阳克","b"=>"西门"); print_r(array_unshift($a,"peter_zhu")); //返回数组长度 echo "<br>"; print_r($a); //输出新的数组 ?>
Output:
3 Array ( [0] => peter_zhu [a] => 欧阳克 [b] => 西门 )
The above is the detailed content of How to use array_unshift function in php. For more information, please follow other related articles on the PHP Chinese website!