PHP 8 reference · Array Functions
PHP array_push() Function
Add one or more elements to the end of an array and return its new length. $array[] = $value does the same for one value.
PHP 8 Since 4.0
Syntax
array_push(array &$array, mixed ...$values): int
Parameters
| Parameter | Description |
|---|---|
array |
The array to add to (changed in place). |
values |
The values to append. |
Return value
The new number of elements.
Examples
<?php
$stack = ["a", "b"];
$length = array_push($stack, "c", "d");
echo $length, "\n";
print_r($stack);
Output
4
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
)<?php
$cart = [];
$cart[] = "tea"; // the usual way to add one value
$cart[] = "cake";
print_r($cart);
Output
Array
(
[0] => tea
[1] => cake
)Version history
| Version | Change |
|---|---|
| PHP 4.0 | Added |
| PHP 7.3 | Can be called with only the array argument. |
In other languages
list.append()· Pythonarray.push()· JavaScript
Related
array_pop()· Remove the last element of an array and return it. Returns null if the array is empty.array_unshift()· Add one or more elements to the start of an array and return its new length. Numeric keys are renumbered.array_merge()· Merge one or more arrays into a new array. Numeric keys are renumbered; for string keys the later value wins.- PHP Arrays · tutorial
- PHP Array Functions · tutorial