← Back to PHP Course | Chapter 7: Arrays | Lesson 5 of 13

PHP Add Array Items

An array often needs to grow after it is first created -- a new item gets added to a cart, a new comment is posted, a new record comes back from a query. PHP gives you several ways to add items to an array: appending to the end, inserting at the start, or setting a brand-new associative key.

Appending with the [] Syntax

$arr[] = $value is the simplest and most common way to append a single new item to an indexed array -- PHP automatically figures out the next available numeric index and assigns the value there, without you needing to track the current length yourself.

Note: Use $arr[] = $value as your default way to add one item at a time, especially inside a loop building up results.

Warning: The [] append syntax always adds to the highest existing numeric key + 1 -- if an array has gaps or unusual keys, the next appended index may not be what you expect.

Example: Appending with the [] Syntax

php
<?php
$fruits = ["Apple", "Banana"];
$fruits[] = "Cherry";
print_r($fruits);
?>

Adding Multiple Items with array_push()

array_push($arr, $value1, $value2, ...) appends one or more values to the end of an array in a single call, which is useful when you already have several new values ready to add at once instead of appending them one at a time.

Note: For a single item, $arr[] = $value is simpler and marginally faster than calling array_push() with one argument.

Warning: array_push() modifies the array it is given directly (by reference) -- it does not return a new array, so there is no need to reassign the result.

Example: Adding Multiple Items with array_push()

php
<?php
$fruits = ["Apple"];
array_push($fruits, "Banana", "Cherry");
print_r($fruits);
?>

Adding Items at the Beginning with array_unshift()

array_unshift($arr, $value1, $value2, ...) inserts one or more values at the very start of an array, shifting every existing item to a higher index -- the opposite end from array_push(), useful for adding a "most recent" item to the front of a list.

Note: Be aware that array_unshift() re-indexes every existing numeric key afterward, so any code relying on specific original index numbers will need to account for the shift.

Warning: array_unshift() is noticeably slower than array_push() on large arrays, since every existing element's index has to shift.

Example: Adding Items at the Beginning with array_unshift()

php
<?php
$queue = ["Bob", "Carol"];
array_unshift($queue, "Alice");
print_r($queue);
?>

Adding a New Associative Key

Adding a new key to an associative array is as simple as assigning a value to a key that does not exist yet: $person["email"] = "[email protected]" creates the "email" key if it was not already present, exactly like a normal assignment.

Note: Check whether a key already exists with isset() first if you specifically need to avoid accidentally overwriting an existing value.

Warning: Because adding and updating a key use identical syntax, PHP will silently overwrite an existing key rather than warn you that you meant to add a brand-new one.

Example: Adding a New Associative Key

php
<?php
$person = ["name" => "Alice"];
$person["email"] = "[email protected]";
print_r($person);
?>

Merging Two Arrays Together

array_merge($arr1, $arr2) combines two arrays into one new array, effectively adding every item from the second array onto the first -- for indexed arrays it re-numbers keys sequentially, while for associative arrays with matching keys, the later array's values win.

Note: Use array_merge() specifically when adding many items from an existing second array at once, rather than looping and appending each one manually.

Warning: array_merge() on two associative arrays with the same key does not combine the values -- the second array's value for that key simply overwrites the first's.

Example: Merging Two Arrays Together

php
<?php
$arr1 = ["a", "b"];
$arr2 = ["c", "d"];
print_r(array_merge($arr1, $arr2));
?>
Common Mistakes
  1. Using $arr[0] = $value to "add" an item, which actually overwrites index 0 if it already exists, instead of appending a genuinely new item.
  2. Forgetting that array_push() and array_unshift() re-index all numeric keys afterward, which can shift indexes you were relying on elsewhere.
  3. Adding items inside a loop with array_push($arr, $item) instead of the simpler $arr[] = $item, which is both shorter and slightly faster for single-item appends.
Chapter Summary
  • $arr[] = $value appends a new item to the end of an indexed array, automatically assigning it the next numeric index.
  • array_push($arr, ...$values) appends one or more values to the end; array_unshift($arr, ...$values) inserts them at the start.
  • Adding a new associative key is as simple as $arr["newKey"] = $value -- if the key does not already exist, it is created.
Browser Support

All array-appending functions and syntax discussed here have been part of PHP since its earliest array support.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.