How To Add To Array In PHP

PHP is an extremely versatile programming language, in many cases, there are an 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 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 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.

PHP example

$arr = array();

array_push($arr, "ArrayValue1");
array_push($arr, "ArrayValue2");
array_push($arr, "ArrayValue3");

print_r($arr);

Output

Array ( [0] => ArrayValue1 [1] => ArrayValue2 [2] => 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

Associative Array

Using the 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.

PHP example 

$assocArr = array();

$assocArr["key1"] = "ArrayVal1";
$assocArr["key2"] = "ArrayVal2";
$assocArr["key3"] = "ArrayVal3";

print_r($assocArr);

Output

Array ( [key1] => ArrayVal1 [key2] => ArrayVal2 [key3] => ArrayVal3 )

Standard Array

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 ones 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 array.

PHP example 

$arr[] = array();

$arr[] = "ArrayValue1";
$arr[] = "ArrayValue2";

print_r($arr);

Output

Array ( [0] => Array ( ) [1] => ArrayValue1 [2] => ArrayValue2 )

Indexed Array

Indexed array’s are again very straight forward, simply replacing the key with an indices 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 a increasing number throughout loops to access arrays data.

PHP Example

$arr[] = array();

$arr[0] = "My Array Val 0";
$arr[1] = "My Array Val 1";
$arr[2] = "My Array Val 2";

print_r($arr);

Output

Array ( [0] => My Array Val 0 [1] => My Array Val 1 [2] => My Array Val 2 )

Multidimensional Array

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.

PHP example

$arr[] = array();

$arr[0][1] = "My Array Val 0-1";
$arr[1][3] = "My Array Val 1-2";
$arr[0][2] = "My Array Val 0-2";
$arr[1][6] = "My Array Val 1-6";

echo $arr[1][3];

Output

My Array Val 1-2

 

Summary

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.

Interesting Links

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.

How To Add To Array In PHP
Posted in PHP

You May Also Like

Leave a Reply

Your email address will not be published.