PHP Add Array Items
PHP Add Array Items
In PHP, you can add array items with several different methods:
[]- adds a single item to the end of an array-
array_push()- adds one or more items to the end of an array array_unshift()- adds one or more items to the beginning of an array-
array_splice()- removes a portion of an array and replaces it with new elements -
array_merge()- merges two or more arrays
Add a Single Array Item
To add a single array item to the end of an existing array, use the bracket [] syntax.
Example
Add one more array item to the $fruits array:
$fruits = array("Apple", "Banana", "Cherry");
$fruits[] = "Orange";
var_dump($fruits);
Try it Yourself »
You can of course add more array items to the end of an existing array, with the bracket [] syntax.
But you have to add one by one:
Example
Add two more array item to the $fruits array:
$fruits = array("Apple", "Banana", "Cherry");
$fruits[] = "Orange";
$fruits[] = "Pear";
var_dump($fruits);
Try it Yourself »
Associative Arrays
To add items to the end of an associative array, use the brackets []
for the key, and assign a value with the = operator.
Example
Add one more array item to the $cars array:
$cars = array("brand" => "Ford", "model" => "Mustang");
$cars["color"] = "Red";
var_dump($cars);
Try it Yourself »
PHP array_push() Function
The
array_push() function is used to add
one or more array items to the end of an existing array.
Example
Add three array items to the end of the $fruits array:
$fruits = array("Apple", "Banana", "Cherry");
array_push($fruits, "Orange", "Kiwi", "Lemon");
var_dump($fruits);
Try it Yourself »
Add Multiple Items to Associative Arrays
To add multiple items to an existing associative array, you can use the
+= operator.
Example
Add two more items to the $cars array:
$cars = array("brand" => "Ford", "model" => "Mustang");
$cars += ["color" => "red", "year" => 1964];
var_dump($cars);
Try it Yourself »
PHP array_unshift() Function
The
array_unshift() function is used to add
one or more array items to the beginning of an existing array.
Example
Add three array items to the beginning of the $fruits array:
$fruits = array("Apple", "Banana", "Cherry");
array_unshift($fruits, "Orange", "Kiwi", "Lemon");
var_dump($fruits);
Try it Yourself »
PHP array_splice() Function
The
array_splice() function is used
remove a portion of an array and replace it with new items.
If you specify an offset and a length of 0 (nothing to remove), you can insert an item at that position.
Example
Insert a new array item at index 1 of the $fruits array:
$fruits = array("Apple", "Banana", "Cherry");
$new_fruit = "Orange";
array_splice($fruits, 1, 0, $new_fruit); // insert "Orange" at index 1
var_dump($fruits);
Try it Yourself »
PHP array_merge() Function
The
array_merge() function is used
to merge two or more arrays.
Example
Merge two arrays:
$fruits1 = array("Apple", "Banana");
$fruits2 = array("Cherry", "Orange");
$result = array_merge($fruits1, $fruits2);
var_dump($result);
Try it Yourself »