← 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.
Syntax
php
$array[] = $value;                      // append
array_push($array, $value1, $value2);   // append several
array_unshift($array, $value);           // add at the start

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
// Declare `$fruits` as an array: `["Apple", "Banana"]`
$fruits = ["Apple", "Banana"];
// Append "Cherry" to the end of `$fruits`
$fruits[] = "Cherry";
// Print a human-readable dump of `$fruits`
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
// Declare `$fruits` as an array: `["Apple"]`
$fruits = ["Apple"];
// Call `array_push($fruits, "Banana", "Cherry")`
array_push($fruits, "Banana", "Cherry");
// Print a human-readable dump of `$fruits`
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
// Declare `$queue` as an array: `["Bob", "Carol"]`
$queue = ["Bob", "Carol"];
// Call `array_unshift($queue, "Alice")`
array_unshift($queue, "Alice");
// Print a human-readable dump of `$queue`
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
// Declare `$person` as an array: `["name" => "Alice"]`
$person = ["name" => "Alice"];
// Set `$person["email"]` to "[email protected]"
$person["email"] = "[email protected]";
// Print a human-readable dump of `$person`
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
// Declare `$arr1` as an array: `["a", "b"]`
$arr1 = ["a", "b"];
// Declare `$arr2` as an array: `["c", "d"]`
$arr2 = ["c", "d"];
// Print a human-readable dump of `array_merge($arr1, $arr2)`
print_r(array_merge($arr1, $arr2));
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
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.