Menu

PHP array_push(): Add Elements to an Array

array_push($arr, $value) adds one or more values to the end of an array; $arr[] = $value does the same for one value and is faster. Learn how to add to the end, the beginning or the middle of an array, add a key and value, and avoid the classic return-value bug.

This page includes runnable editors - edit, run, and see output instantly.

array_push($arr, $value) adds a value to the end of an array. For a single value, the shorthand $arr[] = $value; does the same job without a function call and is what most PHP code uses. Both change the array in place.

array_push() takes the array by reference, so it modifies the variable you pass. It returns the new number of elements, which you can ignore.

array_push vs $arr[] =

Use $arr[] = $value for one value: it is shorter and avoids a function call, which adds up inside a big loop. Use array_push() when you have several values to add in one statement, or a list of values to spread in.

$arr[] = also works on a variable that does not exist yet: PHP creates the array. array_push() needs an existing array: on an undefined variable it throws TypeError: array_push(): Argument #1 ($array) must be of type array, null given.

Add a key and value

array_push() only appends values with the next integer key. To add a named key, assign to it. To add several pairs at once, use += (keeps existing keys) or array_merge() (lets the new values win).

The difference between + and array_merge() when keys collide is covered on the array_merge page.

Add to the beginning: array_unshift

array_unshift($arr, $value) inserts at the start. Integer keys are renumbered so the new value gets index 0; string keys stay as they are. Like array_push(), it accepts several values and returns the new count.

To put a named pair first, build a new array: $menu = ['login' => '/login'] + $menu;.

Insert at a specific position

array_splice($arr, $offset, 0, $values) inserts without removing anything: it starts at $offset, removes 0 elements, and puts the new values there. The integer keys after the insertion point are renumbered.

array_splice() can also remove and replace elements; the page on removing elements shows those forms.

Append another array

array_push($a, $b) adds $b as one nested element, which is rarely what you want. To add the values of $b one by one, spread them, or build a new array with the spread syntax or array_merge().

Common mistake: assigning the return value

array_push() returns a number, the new element count. Assigning that back to the array variable replaces your array with an integer, and the next array_push() on it throws a TypeError.

The same applies to array_unshift() and to sort(), which returns true. Functions that change an array in place give back a status, not the array.

Frequently Asked Questions

What is the difference between array_push and $arr[] in PHP?

Both append to the end. $arr[] = $x; adds one value without a function call, so it is the usual choice and slightly faster. array_push($arr, $a, $b, $c) is handy when you add several values at once, and it returns the new element count.

How do I add a key and value to an array in PHP?

Assign to the key: $arr['color'] = 'blue';. array_push() cannot do this because it always uses the next integer key. To add several pairs at once, use $arr += ['a' => 1, 'b' => 2]; or array_merge().

How do I add an element to the beginning of an array in PHP?

Use array_unshift($arr, $value). It renumbers the integer keys so the new value gets index 0; string keys are kept as they are.

Why does my array become a number after array_push?

Because array_push() returns the new number of elements, not the array. Writing $arr = array_push($arr, 'x'); replaces the array with that number. Call it on its own line: array_push($arr, 'x');.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED