Append “blue” and “yellow” to the end of an array:
| <?php $a=array(“red”,“green”); array_push($a,“blue”,“yellow”); print_r($a); ?> |
The array_push() function adds one or more elements to the end of an array.
Tip: You can add a single value or multiple values.
Note: Even if the array has string keys, the added elements will always have numeric keys (see example below).
| array_push(array, value1, value2, …) |
|
Parameter |
Description |
|
array |
Required. Defines the array. |
|
value1 |
Optional. Specifies the value to add (required in PHP versions prior to 7.3). |
|
value2 |
Optional. Indicates the value to be added. |
|
Return Value: |
Returns the updated count of elements in the array. |
|
PHP Version: |
4+ |
|
Change log: |
Starting from version 7.3, this function can be invoked with just the array parameter. |
An array with string indices:
| <?php $a=array(“a”=>“red”,“b”=>“green”); array_push($a,“blue”,“yellow”); print_r($a); ?> |