PHP is an extremely versatile programming language, in many cases, there is a vast selection of methods to carry out the same task. Adding to an array is one of them, and in this article, I will go through all the options to achieve this common task.
Using the array_push method
If you’ve used other programming languages before, then you will of probably already heard of the term ‘pushing to an array’. The
array_push function is directly responsible for this terminology. It merely pushes a single value to the
array that is specified in the parameters. The first parameter is the array that needs to be pushed to and the second the value. Let’s see an example.
$arr = array(); array_push($arr, "ArrayValue1"); array_push($arr, "ArrayValue2"); array_push($arr, "ArrayValue3"); print_r($arr);
Array (  => ArrayValue1  => ArrayValue2  => ArrayValue3 )
This method of pushing to an
array is probably the most meaningful and understandable in terms of great code, but it’s certainly not the only way to do it. Let’s see some more.
Manually adding to an Array
array_push method may not always float your boat or simply may not fit the purpose of the code, that’s why PHP has other ways to complete this common task. Manually adding to an associative array is another way that allows you to specify keys at the same time as adding values.
This example proves how adaptable adding to arrays can become, especially when working with big data from a database.
$assocArr = array(); $assocArr["key1"] = "ArrayVal1"; $assocArr["key2"] = "ArrayVal2"; $assocArr["key3"] = "ArrayVal3"; print_r($assocArr);
Array ( [key1] => ArrayVal1 [key2] => ArrayVal2 [key3] => ArrayVal3 )
Adding to a standard array is very similar to how you would to an associative one, you will see the obvious similarities to the example above in the following PHP code. There is one con to it though, It’s not as good-looking as
array_push looks in general. Therefore, using this method is at one’s preference.
In the code, we initialize the array before adding, making sure we use  brackets within the variable name for instructing PHP it is to be an.
$arr = array(); $arr = "ArrayValue1"; $arr = "ArrayValue2"; print_r($arr);
Array (  => Array ( )  => ArrayValue1  => ArrayValue2 )
Indexed arrays are again very straight forward, simply replacing the key with an index rather than a string like in the associative example. These type of arrays are handy in many use cases, it just depends if it’s necessary for what you need at the time.
You can see why indexed arrays are heavily used within
for loops, with having an increasing number throughout loops to access arrays data.
$arr = array(); $arr = "My Array Val 0"; $arr = "My Array Val 1"; $arr = "My Array Val 2"; print_r($arr);
Array (  => My Array Val 0  => My Array Val 1  => My Array Val 2 )
Multidimensional arrays are just as easy as indexed arrays, simply adding another ‘key’ to the array variable and specifying the ‘child’ index. PHP has made adding to any type of array extremely straight forward. See the example below.
$arr = array(); $arr = "My Array Val 0-1"; $arr = "My Array Val 1-2"; $arr = "My Array Val 0-2"; $arr = "My Array Val 1-6"; echo $arr;
My Array Val 1-2
As shown in this articles code examples, adding to an array in PHP is super-quick and easy, providing two main programmatic methods to achieve it. At the end of it all, it ultimately comes down to preference and what the task in hand is.
It’s certainly worth noting here that if you want to create an associative or indexed array, that the manual examples are much better. If you try with
array_push, accessing the values further into the code gets pretty tricky and unnecessarily messy. Be sure to check out the array_push documentation though, for simple array population.