PHP array_merge Function | Merge Two or More Arrays

PHP array_merge Function is an inbuilt Function in PHP which merges two or more arrays. This function merges elements in two or more arrays into one single array. While merging, it appends the elements of an array at the end of the previous array. In this article, we will learn about the PHP array_merge Function. Additionally, we will go through a few examples to demonstrate it’s usage.

Syntax

array array_merge($array1, $array2, ……, $arrayn)

Parameters

The PHP array_merge Function takes in a list of array parameters. You can pass any number of parameters in the function. However, all the parameters must be arrays.

If the input arrays contain the same numeric keys, then the PHP array_merge will append the keys and not overwrite them. However, if the arrays contain the same string keys then it overwrites the later value of the keys with the previous one. This will be demonstrated in the examples.

Return Value

The PHP array_merge Function in PHP returns a single array after merging the arrays passed in the parameters.

Values in the input arrays with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

PHP array_merge Function Diagram Concatly

Examples

Let’s go through a few examples to demonstrate the usage of the function.

Example 1: Merging Two Arrays With Integer Keys

<?php
	
	$array1 = array(1,3,5);
	$array2 = array(2,4,6);
	$mergedArray = array_merge($array1, $array2);
	print_r($mergedArray);
	/*
	OUTPUT
	Array
	(
		[0] => 1
		[1] => 3
		[2] => 5
		[3] => 2
		[4] => 4
		[5] => 6
	)
	*/
?>

In the above example, we have two arrays with integer keys and values. The function appends the elements from second array after the first array. Also, it renumbers the keys starting from 0.

Example 2: Merging Two Arrays With String Keys

For instance, consider an example merging two arrays with string keys.

<?php
	
	$array1 = array('dog' => 1, 'cat' => 5);
	$array2 = array('lion' => 5, 'dog' => 3);
	$mergedArray = array_merge($array1, $array2);
	print_r($mergedArray);
	/*
	Array
	(
	    [dog] => 3
	    [cat] => 5
	    [lion] => 5
	)
	*/
?>

The array_merge Function merges the two arrays. However, both the arrays have one same key: ‘dog’. The function overwrites the value of ‘dog’ key from the second array in the final array.

Example 3: Passing Multiple Arrays

For example, consider an example with passing multiple arrays.

<?php
	
	$array1 = array('dog', 'cat', 2, 5);
	$array2 = array('lion' => 5, 'dog' => 3);
	$array3 = array(1, 10);
	$array4 = array(array('name' => 'Concatly'));
	$mergedArray = array_merge($array1, $array2, $array3, $array4);
	print_r($mergedArray);
	/*
	Array
	(
	    [0] => dog
	    [1] => cat
	    [2] => 2
	    [3] => 5
	    [lion] => 5
	    [dog] => 3
	    [4] => 1
	    [5] => 10
	    [6] => Array
	        (
	            [name] => Concatly
	        )

	)
	*/
?>

We can pass multiple arrays to the function. Also, there is no restriction on the data type of the arrays passed.

Example 4: Using Union Operator (+)

Sometimes, the Union Operator is more beneficial than the array_merge Function in PHP. The array_merge function does not preserve numeric key values.  If you need to preserve the numeric keys, then using + will do that.

<?php
	$array1[0] = "zero";
	$array1[1] = "one";

	$array2[1] = "one";
	$array2[2] = "two";
	$array2[3] = "three";

	$array3 = $array1 + $array2;

	print_r($array3);

	print_r(array_merge($array1, $array2));
?>

The output of union operator will be as follows:

Array
(
[0] => zero
[1] => one
[2] => two
[3] => three
)

However, the array_merge Function will return the following:

Array
(
[0] => zero
[1] => one
[2] => one
[3] => two
[4] => three
)

Note: the implicit “array_unique” that gets applied as well.

Conclusion

In conclusion, we discussed PHP array_merge Function. It is used to merge two or more arrays and returns a single array. You can learn more Array Functions in PHP on Concatly!

Additionally, learn more about array_merge Function on Official Documentation of PHP.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *