Array_push () Definition and usage
The array_push () function adds one or more elements (in the stack) 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 is required. Specifies an array.
Value1 is required. Specifies the value to be added.
Value2 is optional. Specifies the value to be added.
Tips and comments
Note: even if the array contains a string key name, the element you add is always a numeric key. (See example 2)
NOTE: If array_push () is used to add a unit to the array, it is better to use $ array [] =, because there is no additional burden to call the function.
Note: If the first parameter is not an array, array_push () will issue a warning. This is different from the behavior of $ var []. The latter creates an array.
Example 1Copy codeThe Code is as follows: <? Php
$ A = array ("Dog", "Cat ");
Array_push ($ a, "Horse", "Bird ");
Print_r ($ );
?>
Output:
Array ([0] => Dog [1] => Cat [2] => Horse [3] => Bird) Example 2
Arrays with string keys:Copy codeThe Code is as follows: <? Php
$ A = array ("a" => "Dog", "B" => "Cat ");
Array_push ($ a, "Horse", "Bird ");
Print_r ($ );
?>
Output:
Array ([a] => Dog [B] => Cat [0] => Horse [1] => Bird)