Add elements to array PHP
Here’s the most straightforward and efficient way to add elements to array PHP.
<?php
$array = [];
$array[] = 'A'; //Pushes A to the array.
$array[] = 'B'; //Pushes B to the array.
$array[] = 'C'; //Pushes C to the array.
//OUTPUT
//[A, B, C]
?>
The article includes other ways of adding items to an array. Stay tuned until the end to get a broad overview of the subject.
Introduction
PHP arrays are dynamic meaning that it doesn’t have fixed size but they can be extended in the runtime. This article is all about different ways of adding elements to an array in PHP. So, let’s begin without any further ado.
#1 – Add elements to array PHP – Square brackets syntax
The square bracket syntax is the most common way to add items to an array in PHP. It also saves the overhead of calling a function.
<?php
$array = [];
$array[] = 'A'; //Pushes A to the array.
$array[] = 'B'; //Pushes B to the array.
$array[] = 'C'; //Pushes C to the array.
//OUTPUT
//[A, B, C]
?>
The same syntax is helpful in adding elements to a multidimensional array.
<?php
$developers = [
"PHP" => [
"Nancy" => ["Experience(years)"=>3,"Skills"=>["Laravel","GIT","HTML","CSS"]],
"Josh" => ["Experience(years)"=>2,"Skills"=>["MVC","GIT","React","Node","CSS"]],
],
];
$developers["PHP"]["Kane"]= ["Experience(years)"=>5,"Skills"=>["GIT","Docker","Angular","Tailwind","CSS","Laravel","Wordpress"]];
print_r($developers["PHP"]);
//OUTPUT
/*
[
"Nancy" => ["Experience(years)"=>3,"Skills"=>["Laravel","GIT","HTML","CSS"]],
"Josh" => ["Experience(years)"=>2,"Skills"=>["MVC","GIT","React","Node","CSS"]],
"Kane" => ["Experience(years)"=>3,"Skills"=>["GIT","Docker","Angular","Tailwind","CSS","Laravel","Wordpress"]]
]
*/
?>
The bracket assigns an array to a new key “Kane”. The array output shows the newly added element. Next, let’s check out the array_push function to add elements to array PHP.
#2 – Add elements to array PHP – Using array_push()
As the name implies, the array_push function pushes elements to array PHP. Let’s revisit the last example with array_push.
<?php
$array = [];
array_push($array, 'A'); //Pushes 'A' to the array.
array_push($array, 'B'); //Pushes 'B' to the array.
array_push($array, 'C'); //Pushes 'C' to the array.
//OUTPUT
//[A, B, C]
?>
The function is equally good for multidimensional array.
<?php
$developers = [
"PHP" => [
"Nancy" => ["Experience(years)"=>3,"Skills"=>["Laravel","GIT","HTML","CSS"]],
"Josh" => ["Experience(years)"=>2,"Skills"=>["MVC","GIT","Docker","Bootstrap","CSS"]],
],
];
$element = ["Experience(years)"=>3,"Skills"=>["GIT","Docker","Angular","Bootstrap","CSS"]];
$developers["PHP"]["Kane"] = []; //Initialize array.
array_push($developers["PHP"]["Kane"], $element);
$developers["PHP"]["Kane"] = $developers["PHP"]["Kane"][0];
print_r($developers);
//OUTPUT
/*
[
"Nancy" => ["Experience(years)"=>3,"Skills"=>["Laravel","GIT","HTML","CSS"]],
"Josh" => ["Experience(years)"=>2,"Skills"=>["MVC","GIT","Docker","Bootstrap","CSS"]],
"Kane" => ["Experience(years)"=>3,"Skills"=>["GIT","Docker","Angular","Bootstrap","CSS"]]
]
*/
?>
There are some extra steps that are absolutely necessary to achieve the desired output here.
- Initialize an empty array for the new key “Kane”. (The function throws an error otherwise)
- Use array push to add elements to the array.
The function does push the item to the array but the output looks off. Here’s how it looks.
[
"Nancy" => ["Experience(years)"=>3,"Skills"=>["Laravel","GIT","HTML","CSS"]],
"Josh" => ["Experience(years)"=>2,"Skills"=>["MVC","GIT","Docker","Bootstrap","CSS"]],
"Kane" => [0] => ["Experience(years)"=>3,"Skills"=>["GIT","Docker","Angular","Bootstrap","CSS"]]
]
An extra array has been added to “Kane”. To get rid of it, we reassign the array to the key as.
$developers["PHP"]["Kane"] = $developers["PHP"]["Kane"][0];
Perfect! Let’s move on to another function that also adds new items to existing array PHP.
#3 – Add elements to array PHP – Using array_unshift()
PHP array_unshift function prepends element to an array unlike the array push.
<?php
$array = [];
array_unshift($array, 'C'); //Pushes 'C' to the array.
array_unshift($array, 'B'); //Pushes 'B' to the array.
array_unshift($array, 'A'); //Pushes 'A' to the array.
//OUTPUT
//[A, B, C]
?>
See the elements are added in the reverse order because the function always adds to the start of an array.
This function works the same way for the multidimensional arrays as the array_push function as seen already in the last example.
#4 – Add elements to array PHP – Using array_merge()
PHP array_merge function is also helpful in adding more than one element at a time. Here’s how it goes.
<?php
$array = [];
$array = array_merge($array, ['A', 'B', 'C']);
print_r($array);
//OUTPUT
//[A, B, C]
?>
Voila! Works just fine. The function overwrites similar keys, so always make sure what you’re up to. FuelingPHP features an article about the PHP array merge function that can help you master this function.
Here’s array_merge for the multidimensional array.
<?php
$developers = [
"PHP" => [
"Nancy" => ["Experience(years)"=>3,"Skills"=>["Laravel","GIT","HTML","CSS"]],
"Josh" => ["Experience(years)"=>2,"Skills"=>["MVC","GIT","Docker","Bootstrap","CSS"]],
],
];
$element = ["Experience(years)"=>3,"Skills"=>["GIT","Docker","Angular","Bootstrap","CSS"]];
$developers = array_merge($developers["PHP"], ["Kane" => $element]);
print_r($developers);
//OUTPUT
/*
[
"Nancy" => ["Experience(years)"=>3,"Skills"=>["Laravel","GIT","HTML","CSS"]],
"Josh" => ["Experience(years)"=>2,"Skills"=>["MVC","GIT","Docker","Bootstrap","CSS"]],
"Kane" => ["Experience(years)"=>3,"Skills"=>["GIT","Docker","Angular","Bootstrap","CSS"]]
]
*/
?>
All set! That’s all for this article. Time for a wrap up.
Wrap Up
The article explains multiple ways of adding items to an array in PHP. The most robust way is using the square bracket syntax as it saves a function overhead. The following methods – array push, unshift and merge are also useful and have been demonstrated through examples.
Hopefully, you’ve liked the article. Stay tuned for more at FuelingPHP.
Want to learn more about PHP?
We have many fun articles related to PHP. You can explore these to learn more about PHP.