How To Append Array In Php

Arrays are an essential part of any programming language, and PHP is no exception. An array, in simple terms, is
a collection of variables stored in a single container. In PHP, there are different ways to work with arrays,
and one of the most common operations is appending elements to an existing array.

In this blog post, we will explore various methods to append elements to an array in PHP. Let’s dive in!

1. Using the Array Push Function

The simplest and most common method to append an element to an array is using the array_push()
function. This built-in PHP function accepts the array to which you want to append an element and the value(s)
you wish to append.

Here’s an example of how to use the array_push() function:


        <?php
        $fruits = ['apple', 'banana', 'cherry'];
        array_push($fruits, 'orange');
        print_r($fruits);
        ?>
    

The output of the above code will be:


        Array
        (
            [0] => apple
            [1] => banana
            [2] => cherry
            [3] => orange
        )
    

2. Using the Square Bracket Syntax

Another way to append elements to an array is by using the square bracket syntax. To use this method, simply
reference the array with an empty set of square brackets and assign the value you want to append.

Here’s an example of how to append an element to an array using the square bracket syntax:


        <?php
        $fruits = ['apple', 'banana', 'cherry'];
        $fruits[] = 'orange';
        print_r($fruits);
        ?>
    

The output of the above code will be the same as the previous example:


        Array
        (
            [0] => apple
            [1] => banana
            [2] => cherry
            [3] => orange
        )
    

3. Combining Arrays with the Array Merge Function

You can also append multiple elements to an array by merging two arrays using the array_merge()
function. This function takes two or more arrays as arguments and returns a new array containing the elements
of the input arrays combined.

Here’s an example of how to append multiple elements to an array using the array_merge()
function:


        <?php
        $fruits = ['apple', 'banana', 'cherry'];
        $moreFruits = ['orange', 'grape'];
        $allFruits = array_merge($fruits, $moreFruits);
        print_r($allFruits);
        ?>
    

The output of the above code will be:


        Array
        (
            [0] => apple
            [1] => banana
            [2] => cherry
            [3] => orange
            [4] => grape
        )
    

Conclusion

In this blog post, we discussed three different methods to append elements to an array in PHP: using the
array_push() function, the square bracket syntax, and the array_merge()
function. Each method has its own use cases, depending on whether you want to append a single element or merge
multiple arrays.

We hope this post has helped you grasp the various ways to append elements to an array in PHP. Happy coding!