PHP Add Array Items
In this page:
$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.
Example: Appending with the [] Syntax
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
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.
Example: Adding Multiple Items with array_push()
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
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.
Example: Adding Items at the Beginning with array_unshift()
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
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.
Example: Adding a New Associative Key
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
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.
Example: Merging Two Arrays Together
<?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));
?>
Login to try C/C++/Java/PHP code in the editor
- Using $arr[0] = $value to "add" an item, which actually overwrites index 0 if it already exists, instead of appending a genuinely new item.
- Forgetting that array_push() and array_unshift() re-index all numeric keys afterward, which can shift indexes you were relying on elsewhere.
- 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.
- $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.
All array-appending functions and syntax discussed here have been part of PHP since its earliest array support.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: