PHP Array Push Function: A Comprehensive Guide
The PHP array push function is a crucial tool for web developers who need to add elements to arrays in their PHP scripts. This function allows you to add one or
array_push() adds one or more elements to the end of an array, treating that array like a stack. It is the everyday way to grow a list in PHP, and it returns the new number of elements once the values have been added.
This page covers the syntax, the value array_push() returns, how it behaves with associative and empty arrays, the faster [] shorthand, and the related functions for removing or prepending elements.
Syntax
array_push(array &$array, mixed ...$values): int$array— the array to modify. It is passed by reference (&), so the original variable is changed in place; you don't reassign it.$values— one or more values to append, in the order given.- Return value — the new length of the array (an integer).
Basic example
The example below starts with three fruits and appends two more:
The new values are added in order, and $count holds the updated length:
5
Array
(
[0] => apple
[1] => banana
[2] => cherry
[3] => orange
[4] => peach
)The [] shorthand for a single value
If you only need to append one element, the $array[] = $value syntax does the same thing and is faster, because it avoids the overhead of a function call:
<?php
$colors = array("red", "green");
$colors[] = "blue"; // same as array_push($colors, "blue")
print_r($colors);
?>Array
(
[0] => red
[1] => green
[2] => blue
)Reach for array_push() when you need to add several values at once; use [] for a single value.
Behavior with keys
array_push() always re-indexes the appended values with new integer keys, continuing from the array's highest existing integer key. Existing string (associative) keys are left untouched:
<?php
$data = array("name" => "Ann", 5 => "five");
array_push($data, "added");
print_r($data);
?>Array
(
[name] => Ann
[5] => five
[6] => added
)Notice the new element became [6] — one past the largest integer key (5), not [2].
Pushing onto an empty array
You don't need to declare keys first. Pushing onto an empty array starts the numbering at 0:
<?php
$list = array();
array_push($list, "first", "second");
print_r($list);
?>Array
(
[0] => first
[1] => second
)Common gotchas
- It modifies in place. Because the array is passed by reference, do not write
$arr = array_push($arr, $x)— that would overwrite your array with the integer length. Callarray_push($arr, $x)on its own line. - The argument must be a variable. Since the parameter is by reference, you cannot push onto a literal like
array_push(array(), "x"). - Use
[]for one value. For a single element,$arr[] = $valueis clearer and a bit faster.
Related functions
array_pop()— removes and returns the last element (the opposite of pushing).array_unshift()— adds elements to the beginning of an array.array_shift()— removes the first element.array_merge()— combines two or more arrays into one.array_splice()— inserts or removes elements at any position.