Array_push () Definition and usage
The Array_push () function adds one or more elements (into the stack) to the end of the array of the first argument, and then returns the length of the new array.
This function equals multiple calls to the $array [] = $value.
Grammar
Array_push (Array,value1,value2 ...) Parameter description
Array required. Specify an array.
Value1 required. Specify the value to add.
Value2 Optional. Specify the value to add.
Tips and comments
Note: Even if there is a string key in the array, the element you are adding is always a numeric key. (see Example 2)
Note: If you use Array_push () to add a cell to an array, you might as well use $array [] =, because there is no additional burden to invoke the function.
Note: If the first argument is not an array, Array_push () will issue a warning. This differs from the behavior of $var [], which creates a new array.
Example 1
Copy Code code 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
An array with a string key:
Copy Code code 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)