In this tutorial, you shall learn about PHP array_push() function which can add one or more elements to an array, with syntax and examples.
PHP array_push() Function
The PHP array_push() function pushes (appends) one or more elements to the end of given array.
PHP array_push() modifies the original array to which we are appending the values.
Syntax of array_push()
The syntax of PHP array_push() function is
</>
Copy
array_push( array, value1, value2, ...)
where
| Parameter | Description |
|---|---|
| array | [mandatory] An array to which we append/insert/push values at the end of it. |
| value1 | [optional] The value to append to the array. |
| value2 | [optional] Another value to append to array at end after value1. |
| … | [optional] More values to append to the array. |
Function Return Value
PHP array_push() function returns the number of elements in the new updated array appended with values.
Examples
1. Push/Append Value to Array
In this example, we will take an array with two values. We will use array_push() function to append a value to this array.
PHP Program
</>
Copy
<?php
$array1 = array("apple", "banana");
array_push($array1, "mango");
print_r($array1);
?>
Output
