How to Append One Array to Another in PHP?

Arrays are fundamental data structures in PHP, and you often need to combine or append them to create larger arrays or merge data from different sources.

In this article, we will explore various methods for appending one array to another in PHP.

1. Using the array_merge() Function

The array_merge() function in PHP is a straightforward way to append one array to another. It takes two or more arrays as arguments and returns a new array that is a combination of all the arrays.

Here's an example of how to use array_merge() to append one array to another:

$array1 = ["apple", "banana", "cherry"];
$array2 = ["date", "fig", "grape"];

$result = array_merge($array1, $array2);

print_r($result);

Output:

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

In this example, we have two arrays, $array1 and $array2. The array_merge() function combines them into a single array called $result.


2. Using the + Operator

PHP allows you to use the + operator to merge arrays. When you use the + operator between two arrays, it appends the elements from the second array to the first array, but only if the keys in the second array do not already exist in the first array. This prevents overwriting existing values.

Here's an example:

$array1 = ["a" => "apple", "b" => "banana", "c" => "cherry"];
$array2 = ["b" => "blueberry", "d" => "date", "e" => "elderberry"];

$result = $array1 + $array2;

print_r($result);

Output:

Array
(
    [a] => apple
    [b] => banana
    [c] => cherry
    [d] => date
    [e] => elderberry
)

In this example, we have two arrays, $array1 and $array2. When we use the + operator to merge them, the keys "b," "d," and "e" from $array2 are added to $array1, but the existing values for "b" and "c" in $array1 are not overwritten.


3. Using the array_push() Function

The array_push() function in PHP is typically used to add one or more elements to the end of an array.

However, you can use it to append one array to another by iterating through the second array and pushing each element onto the first array.

Here's an example:

$array1 = ["apple", "banana", "cherry"];
$array2 = ["date", "fig", "grape"];

foreach ($array2 as $element) {
    array_push($array1, $element);
}

print_r($array1);

Output:

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

In this example, we use a foreach loop to iterate through $array2, and for each element, we use array_push() to add it to the end of $array1. This effectively appends $array2 to $array1.


4. Using the array_splice() Function

The array_splice() function in PHP can also be used to append one array to another.

It allows you to insert the elements from one array into another array at a specified position.

Here's an example:

$array1 = ["apple", "banana", "cherry"];
$array2 = ["date", "fig", "grape"];

array_splice($array1, count($array1), 0, $array2);

print_r($array1);

Output:

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

In this example, we use array_splice() to insert the elements from $array2 at the end of $array1.

The count($array1) argument specifies the position at which to insert the elements (at the end), and 0 specifies that no elements should be removed from $array1.


Conclusion

Appending one array to another is a common operation in PHP, and there are multiple methods to achieve it. You can choose the method that best suits your needs and coding style. Whether you prefer using array_merge(), the + operator, array_push(), or array_splice(), understanding these techniques will help you effectively manipulate and combine arrays in your PHP applications.