array_push()

array_push() — Push one or more elements onto the end of array.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed. Has the same effect as: <?php $array[] = $var; ?> repeated for each passed value.

Signature

array_push(array &$array, mixed ...$values): int

Parameters

ParameterTypeWhat it takes
&$arrayarrayThe input array.
...$valuesmixedThe values to push onto the end of the array.

Return value

Returns the new number of elements in the array.

Example

A runnable snippet:

$stack = array("orange", "banana"); array_push($stack, "apple", "raspberry"); print_r($stack);

Output:

Array ( [0] => orange [1] => banana [2] => apple [3] => raspberry )

Changelog

VersionChange
7.3.0This function can now be called with only one parameter. Formerly, at least two parameters have been required.

See also

array_pop(), array_shift(), array_unshift()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page array_push() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.