Home > Article > Backend Development > How to use array_push function in php
Usage of array_push function in php: [array_push(array,value1)]. The array_push function is used to add one or more elements to the end of the specified array and return the length of the new array.
php The array_push function is used to add one or more elements (push) to the end of the array of the first parameter, and then return the length of the new array. The syntax is array_push(array,value1,value2...), the parameter array is required and specifies the array; value1 is required and specifies the value to be added.
(Recommended tutorial: php video tutorial)
How to use the php array_push function?
Function: Add one or more elements (push) to the end of the array of the first parameter, and then return the length of the new array.
Syntax:
array_push(array,value1,value2...)
Parameters:
array Required. Specifies an array.
value1 Required. Specifies the value to add.
value2 Optional. Specifies the value to add.
Note:
Even if there are string key names in the array, the elements you add are always numeric keys. If you use array_push() to add a unit to an array, it is better to use $array[] =, because there is no additional burden of calling the function. array_push() will issue a warning if the first argument is not an array. This is different from the behavior of $var[], which creates a new array.
php array_push() function usage example 1
<?php $a=array("西门","灭绝"); print_r(array_push($a,"无忌","黄蓉"));//返回新数组长度 echo "<br>"; print_r($a);//打印新数组 ?>
Output:
4 Array ( [0] => 西门 [1] => 灭绝 [2] => 无忌 [3] => 黄蓉 )
php array_push() function usage example 2
<?php $a=array("无忌","欧阳克"); print_r(array_push($a,"灭绝"));//返回新数组长度 echo "<br>"; print_r($a);//打印新数组 ?>
Output:
3 Array ( [0] => 无忌 [1] => 欧阳克 [2] => 灭绝 )
The above is the detailed content of How to use array_push function in php. For more information, please follow other related articles on the PHP Chinese website!