PHP array_merge() Tutorial 🎯

beginner
14 min

PHP array_merge() Tutorial 🎯

Welcome to our comprehensive guide on the PHP array_merge() function! This function is a powerful tool that lets you merge arrays together. Let's dive in and understand it from the ground up.

What is array_merge()? πŸ“

In PHP, array_merge() is a built-in function that combines one or more arrays. It returns a new array with elements from all the input arrays merged together.

Syntax πŸ’‘

The basic syntax for array_merge() is as follows:

php
array array_merge(array ...$arrays)

You can pass multiple arrays as arguments to the array_merge() function, and it will return a single array containing all the elements from the input arrays.

Merging Arrays πŸ’‘

Let's look at an example to understand how array_merge() works:

php
<?php $array1 = array("apple", "banana", "cherry"); $array2 = array("mango", "orange", "grape"); $mergedArray = array_merge($array1, $array2); print_r($mergedArray); ?>

Output:

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

In this example, we have two arrays $array1 and $array2. We use the array_merge() function to combine these arrays and store the result in the $mergedArray.

Multidimensional Arrays πŸ’‘

array_merge() can also be used with multidimensional arrays. Here's an example:

php
<?php $array1 = array( "fruits" => array("apple", "banana", "cherry"), "vegetables" => array("carrot", "potato", "onion") ); $array2 = array( "fruits" => array("mango", "orange", "grape"), "vegetables" => array("beans", "corn", "lettuce") ); $mergedArray = array_merge($array1, $array2); print_r($mergedArray); ?>

Output:

Array ( [fruits] => Array ( [0] => apple [1] => banana [2] => cherry [3] => mango [4] => orange [5] => grape ) [vegetables] => Array ( [0] => carrot [1] => potato [2] => onion [3] => beans [4] => corn [5] => lettuce ) )

In this example, we have two multidimensional arrays $array1 and $array2. We use the array_merge() function to combine these arrays, and the result is a single multidimensional array containing all the elements from the input arrays.

Overlapping Elements πŸ’‘

When merging arrays that contain overlapping elements, array_merge() will overwrite the overlapping elements. Here's an example:

php
<?php $array1 = array("apple", "banana", "cherry", "apple"); $array2 = array("mango", "orange", "grape", "banana"); $mergedArray = array_merge($array1, $array2); print_r($mergedArray); ?>

Output:

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

In this example, we have two arrays $array1 and $array2 with overlapping elements banana. The array_merge() function overwrites the overlapping elements, resulting in the final array with the last occurrence of each overlapping element.

Quiz 🎯

Quick Quiz
Question 1 of 1

Which PHP function is used to merge arrays together?

Recap βœ…

In this tutorial, we've learned about the PHP array_merge() function, which is used to combine arrays. We've covered its syntax, usage with single and multidimensional arrays, and handling overlapping elements.

Now that you've learned about the array_merge() function, you can create more complex and efficient PHP scripts by merging arrays effectively. Happy coding! πŸŽ‰