The array_push() function is used to add one or more elements onto the end of an array. The length of array increases by the number of variables pushed.
Tip: You can add one value, or as many as you like.
Note: Even if your array has string keys, your added elements will always have numeric keys.
Parameter | Description |
---|---|
array | Required. Specifies an array |
value1 | Required. Specifies the value to add |
value2 | Optional. Specifies the value to add |
Syntax
array_push(array,value1,value2...)
Example
$array=array("Samuel","Cooper");
array_push($array,"Prince","Alex");
print_r($array);
Output
Array ( [0] => Samuel [1] => Cooper [2] => Prince [3] => Alex )
More Example
$array=array("a"=>"Samuel","b"=>"Cooper");
array_push($array,"Prince","Alex");
print_r($array);
Output
Array ( [a] => Samuel [b] => Cooper [0] => Prince [1] => Alex )