PHP: Append an element to the end of an array.

This is a beginner’s tutorial on how to append an element to the end of a PHP array. In this guide, I will show you 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 commonly-used method involves placing two square brackets at the end of the array variable. This is then followed up with the assignment operator (=) and the variable that you want to append to the end of that array.

Take a look at the example below:

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 run the example above on your own server, you will get the following result:

Adding an element to the end of a PHP array

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:

The code above creates a multidimensional array because it adds the second array as an element at the end of our first array:

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. Although, 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:

In the code above, we created a PHP array containing the numbers between 1 and 4. After that, we appended 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. i.e. It modifies the array directly. This means that you should not make the mistake of assigning its return value to your array variable:

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 above piece of code 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:

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 shouldn’t get too 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:

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.

Facebook Comments