In this tutorial, we will show you how to add an element to the end of a PHP array.
We will also outline the two different methods that you can use in order to do this.
Using square brackets to add an element to a PHP array.
The most common method involves placing two square brackets at the end of the array variable. This is then followed by the assignment operator (=) and the variable that you want to add to the end of that array.
Take a look at the example below:
//Our example array $countries = array( 'United States', 'India', 'United Kingdom' ); //Now, let's append Ireland onto the end //of the array $countries = 'Ireland'; //var_dump the array out for example purposes var_dump($countries);
In the snippet above, we created a PHP array that contains the names of three different countries. Then, below our array declaration, we used our square brackets to dynamically append the string “Ireland” onto the end of the array.
If you run the example above on your own server, you will get the following result:
The var_dump in the screenshot above shows that the string “Ireland” was successfully appended to the end of our $countries array. Perfect!
Note that this will work with any kind of variable.
For example, you can also append other kinds of variables, such as arrays or objects:
//Array of numbers $arr = array( 1, 2, 3, 4 ); //Our second array $secondArr = array( 'A', 'B', 'C' ); //Add the second array onto the end of our //first array $arr = $secondArr; //var_dump the array out for example purposes var_dump($arr);
The code above creates a multidimensional array because it adds the second array as an element at the end of our first array:
array (size=5) 0 => int 1 1 => int 2 2 => int 3 3 => int 4 4 => array (size=3) 0 => string 'A' (length=1) 1 => string 'B' (length=1) 2 => string 'C' (length=1)
As you can see, the variable in question does not have to be a scalar type.
Using the array_push function.
PHP also has an inbuilt function called array_push. The array_push function pretty much works the exact same way as the square bracket method.
However, there are some slight differences in their behavior.
As the name suggests, array_push allows you to PUSH a variable onto the end of an array:
//Array of numbers $numbers = array( 1, 2, 3, 4 ); //Push the integer 5 onto the //end of our array array_push($numbers, 5); //Print it out var_dump($numbers);
In the code above, we created a PHP array containing the numbers between 1 and 4. After that, we added the number 5 onto the end of that array by using the array_push function.
Note that array_push takes the array in as a reference. In other words, it modifies the array directly. This means that you should never assign the return value of array_push to your array variable:
//Do NOT do this. $numbers = array_push($numbers, 5);
The array_push function will always return the new length of the array. In this case, it will return the number 5. As a result, the code above will actually overwrite our original array variable with the integer 5.
Not good. Not good at all.
You can also use the array_push function to add multiple elements at the same time:
//Array of numbers $numbers = array( 1, 2, 3, 4 ); //Push 5, 6 and 7 onto the end //of our PHP array array_push($numbers, 5, 6, 7); //var_dump the new array var_dump($numbers);
Here, we were able to add three new elements to the $numbers array with one function call.
Should I use the square brackets method or array_push?
If you are only appending one element to your array, then I would suggest that you use the square brackets method. This is because $a =  does not carry the same overhead as a function call.
However, this isn’t something that you should get stressed about, as any differences will be marginal at best. If array_push is the thin line between your PHP app being fast or slow, then you obviously have much bigger fish to fry.
One small difference between these two methods is how they respond when the array in question does not exist:
$nonExistentArray = 5; array_push($nonExistentArray2, 5); var_dump($nonExistentArray); var_dump($nonExistentArray2);
If the array does not exist, the square brackets method will actually create the new array. In the code above, $nonExistentArray will become an array with the number 5 as its only element.
On the other hand, the function array_push will throw a warning message:
Warning: array_push() expects parameter 1 to be array, null given
It is worth noting that array_push will not create the array either. Instead, the $nonExistentArray2 variable will remain as a null value.
In conclusion, there’s not too much of a difference between the two. Feel free to choose the method that suits you best.