array_push() Definition and Usage The
array_push() function adds one or more elements (push) to the end of the array of the first parameter and returns the length of the new array.
This function is equivalent to calling $array[] = $value multiple times.
Syntax
array_push(array,value1,value2...) Parameter Description
array Required. Specifies an array.
value1 required. Specifies the value to add.
value2 optional. Specifies the value to add.
Tips and Notes
Note: Even if there are string key names in the array, the elements you add are always numeric keys. (See Example 2)
Note: If you use array_push() to add a unit to the array, it is better to use $array[] =, because there is no additional burden of calling the function.
Note: 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.
Example 1
The code is as follows:
<?php $a=array("Dog","Cat"); array_push($a,"Horse","Bird"); print_r($a); ?>
Output:
Array ([0] => Dog [1] => Cat [2] => Horse [3] => Bird )
Example 2
Array with string keys:
Code is as follows:
<?php $a=array("a"=>"Dog","b"=>"Cat"); array_push($a,"Horse","Bird"); print_r($a); ?>
Output:
Array ( [a] => Dog [b] => Cat [0] => Horse [1] => Bird )
The above introduces the array_push of the PHP array function sequence, which adds one or more elements (push) to the end of the array and returns the new length. , including relevant content, I hope it will be helpful to friends who are interested in PHP tutorials.