PHP Add Array Items
PHP offers several ways to put new items into an array, and the right one depends on where the items should go and how many there are:
[]- appends one item at the endarray_push()- appends one item or several at the endarray_unshift()- places one item or several at the startarray_splice()- cuts out part of an array and puts new elements in its placearray_merge()- combines two or more arrays into one
Add a Single Array Item
The bracket syntax [] is the quickest way to append one item to an array that already exists.
Add one item using bracket syntax
Append "Orange" to $fruits:
Output: PHP script
<?php
$fruits = array("Apple", "Banana", "Cherry");
$fruits[] = "Orange";
var_dump($fruits);
?>
Add multiple items one by one
Each bracket assignment adds exactly one item, so several new items take several lines:
Output: PHP script
<?php
$fruits = array("Apple", "Banana", "Cherry");
$fruits[] = "Orange";
$fruits[] = "Pear";
var_dump($fruits);
?>
Associative Arrays
An associative array needs a key for the new item. Write that key inside the brackets [] and give it a value with the = operator. The item lands at the end of the array.
Add item to associative array
Give $cars a new color item:
Output: PHP script
<?php
$cars = array("brand" => "Ford", "model" => "Mustang");
$cars["color"] = "Red";
var_dump($cars);
?>
PHP array_push() Function
When you want to append one item or several in one go, call array_push() and list them after the array.
Using array_push() to add items
Append three fruits to $fruits with a single call:
Output: PHP script
<?php
$fruits = array("Apple", "Banana", "Cherry");
array_push($fruits, "Orange", "Kiwi", "Lemon");
var_dump($fruits);
?>
Add Multiple Items to Associative Arrays
The += operator lets you add several key and value pairs to an associative array in one statement.
Add multiple associative items with += operator
Extend $cars with both a color and a year:
Output: PHP script
<?php
$cars = array("brand" => "Ford", "model" => "Mustang");
$cars += ["color" => "red", "year" => 1964];
var_dump($cars);
?>
PHP array_unshift() Function
array_unshift() works like array_push() but at the other end: the items you pass go in front of what the array already holds, whether that is one item or more.
Using array_unshift() to add items
Put three fruits at the front of $fruits:
Output: PHP script
<?php
$fruits = array("Apple", "Banana", "Cherry");
array_unshift($fruits, "Orange", "Kiwi", "Lemon");
var_dump($fruits);
?>
PHP array_splice() Function
array_splice() takes a part out of an array and can put new items in its place.
Give it a position and a length of 0, so nothing gets removed, and the call simply inserts the new item at that position.
Insert item at specific position
Insert "Orange" at index 1 of $fruits, pushing the later items along:
Output: PHP script
<?php
$fruits = array("Apple", "Banana", "Cherry");
$new_fruit = "Orange";
array_splice($fruits, 1, 0, $new_fruit);
var_dump($fruits);
?>
PHP array_merge() Function
To join two or more arrays into one, pass them to array_merge().
Merge two arrays
Combine $fruits1 and $fruits2 into a new array:
Output: PHP script
<?php
$fruits1 = array("Apple", "Banana");
$fruits2 = array("Cherry", "Orange");
$result = array_merge($fruits1, $fruits2);
var_dump($result);
?>