PHP array merge tutorial with example

PHP array merge tutorial with example. Best tutorial for learn PHP single dimensional, multidimensional, associative and all type of array merge process with example.

Well! what will you learn from this PHP array_merge tutorial? You will learn following array merge process: PHP array_merge, PHP array_merge and PHP array_combine, PHP array merge without duplicate / PHP array merge unique, PHP array_merge order, PHP array_merge_recursive, PHP array_merge subarray, PHP merge multidimensional array, PHP array merge associative, PHP array_merge vs + array union operator, PHP array_merge shorthand, PHP array merge without changing key, PHP array_merge preserve keys, PHP merge associative array by key, PHP merge associative array with duplicate keys, PHP merge multidimensional associative array, PHP array merge Reference sources and Conclusion.

Oh, pretty much long list for PHP array merge, It includes all array merge process. I guaranteed, you will be Ninja in merging PHP array process.

PHP array_merge tutorial with example
PHP array_merge tutorial with example

Lets start PHP array_merge tutorial step by step, you will definitely enjoy and may be bookmarked it for future reference.

PHP array merge associative, multidimensional and simple array tutorial with example
Array merge type index Type of PHP array merge Array function
1 PHP array_merge array_merge
2 PHP array_merge and PHP array_combine array_merge, array_combine
3 PHP array merge without duplicate / PHP array merge unique array_unique
4 PHP array_merge order array_merge
5 PHP array_merge_recursive array_merge_recursive
6 PHP array_merge subarray call_user_func_array, array_merge
7 PHP merge multidimensional array array_merge, array_merge_recursive
8 PHP array merge associative union operator (+), array_merge, array_merge_recursive
9 PHP array_merge vs + array union operator union operator (+)
10 PHP array_merge shorthand union operator (+)
11 PHP array merge without changing key union operator (+)
12 PHP array_merge preserve keys union operator (+)
13 PHP merge associative array by key array_merge_recursive
14 PHP merge associative array with duplicate keys array_merge_recursive
15 PHP merge multidimensional associative array array_merge_recursive
16 PHP array merge Reference sources Reference sources
17 Conclusion Conclusion
PHP array merge associative array, multidimensional array and simple array tutorial with example

PHP array_merge


What is array_merge in PHP? Answer is: array_merge – Merge one or more arrays.

Lets check what instruction given in PHP documentation.

PHP array_merge syntax


array array_merge ( array $array1 [, array $... ] )

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended.

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

Note: Don’t use array_merge function to merge associative array. when your array have numeric keys, array_merge consider it as normal index and replace with normal order number, also if your arrays have same keys array_merge() only use one in result output array.

PHP array_merge example

Here is example which use array_merge function and all described rules applied in process, so you will able to understand how function work and what will be output in different situation.

Example 1: Merge two arrays with mix pattern, normal and associative.


<?php
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
print_r($result);
?>

// output

Array
(
    [color] => green
    [0] => 2
    [1] => 4
    [2] => a
    [3] => b
    [shape] => trapezoid
    [4] => 4
)

Example 2: Numeric keys will be renumbered.


<?php
$array1 = array();
$array2 = array(1 => "data");
$result = array_merge($array1, $array2);
?>

// output

Array
(
    [0] => data
)

Example 3: array merge with non-array types.


<?php
$beginning = 'foo';
$end = array(1 => 'bar');
$result = array_merge((array)$beginning, (array)$end);
print_r($result);
?>

// output

Array
(
    [0] => foo
    [1] => bar
)

PHP array_merge and PHP array_combine


PHP array_merge is just what you seen before this section: PHP array_merge. array_merge – Merge one or more arrays.

And now what is array_combine? answer is array_combine — Creates an array by using one array for keys and another for its values.

PHP array_merge syntax


array array_combine ( array $keys , array $values )

Creates an array by using the values from the keys array as keys and the values from the values array as the corresponding values.

PHP array_combine example

Here is example for array_combine function to combine keys and values array.

Example 4: simple array_combine example.


<?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?>

// output

Array
(
    [green]  => avocado
    [red]    => apple
    [yellow] => banana
)

PHP array merge without duplicate / PHP array merge unique


How to merge two PHP arrays which have some duplicate values so result output array is unique and no duplicate values exist in array. Simply PHP array merge without duplicate / PHP array merge unique.

Simply apply array_unique function after array_merge. array_unique will remove duplicate values entry from array.

What is array_unique? Answer is array_unique — Removes duplicate values from an array.

Example 5: PHP array merge without duplicate.


<?php
$array1 = array(100, 101, 102);
$array2 = array(102, 103, 104);

$array3 = array_unique( array_merge($array1, $array2) );
>

// output

Array
(
    [0] => 100
    [1] => 101
    [2] => 102
    [3] => 103
    [4] => 104
)

PHP array_merge order


What is array_merge output array order? well it based on given parameter order. Output array first elements taken from first array_merge() argument.

array_merge() will be renumbered numeric keys but it generate order based on function argument array.

Example 5: PHP array_merge order.


<?php
$array1 = array(10, 11, 12);
$array2 = array(102, 103, 104);

$array3 = array_merge($array1, $array2);
>

// output

Array
(
    [0] => 10
    [1] => 11
    [2] => 12
    [3] => 102
    [4] => 103
    [5] => 104
)

PHP array_merge_recursive


What is array_merge_recursive? and for what purpose it used?

array_merge_recursive — Merge one or more arrays recursively. It will used to merge array elements recursively, so it can use to merge multidimensional array.

array_merge_recursive() merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.

PHP array_merge_recursive syntax


array array_merge_recursive ( array $array1 [, array $... ] )

Example 6: PHP array_merge_recursive order.


<?php
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite" => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
print_r($result);
?>

// output

Array
(
    [color] => Array
        (
            [favorite] => Array
                (
                    [0] => red
                    [1] => green
                )

            [0] => blue
        )

    [0] => 5
    [1] => 10
)

PHP array_merge subarray


Here array merge subarray means, one main array have sub array with same pattern and now you want to move all subarrays elements to root level, right?

Ok, here is one example for merging sub arrays into main array, you can check it, if it works as per your requirement.

This is array_merge() usage example to move subarray elements to root level in main array.

Example 7: PHP array_merge subarray.


<?php
$array = array(
	array('A','B'),
	array('C','B'),
);

$merged = array_unique(call_user_func_array('array_merge', $array));
print_r($merged);
?>

// output

Array ( [0] => A [1] => B [2] => C )

PHP merge multidimensional array


PHP merge multidimensional array. If you looking for merge simple multidimensional array which contains same pattern child arrays, you can do it using array_merge() as like below example.

Example 8: PHP array_merge for simple multidimensional array.


<?php
$array1 = array(
	array('id' => 1,'name' => 'name1'),
	array('id' => 2,'name' => 'name2'),
);

$array2 = array(
	array('id' => 3,'name' => 'name3'),
	array('id' => 4,'name' => 'name4'),
);

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

// output

Array (
    [0] =>  Array ( [id] => 1 [name] => name1 ) 
    [1] => Array ( [id] => 2 [name] => name2 ) 
    [2] => Array ( [id] => 3 [name] => name3 ) 
    [3] => Array ( [id] => 4 [name] => name4 ) 
)

Here you can also use PHP array_merge_recursive if you have more inner level array.

TIP: If you tried all thing and not able to found solution for multidimensional array merge, one more time visit array_merge_recursive official document page, and check “User Contributed Notes” you will find something helpful.

PHP array merge associative


PHP array_merge associative. As we discussed in array_merge section that:

Don’t use array_merge function to merge associative array. when your array have numeric keys, array_merge consider it as normal index and replace with normal order number, also if your arrays have same keys array_merge() only use one in result output array.

So what should you use instead array_merge() for associative array? Answer is PHP array union operator (+).

If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator.

Example 9: PHP array merge associative array.

The keys from the first array will be preserved. If an array key exists in both arrays, then the element from the first array will be used and the matching key’s element from the second array will be ignored.

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.


<?php
$array1 = array(0 => 'zero_a', 2 => 'two_a', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
$result = $array1 + $array2;
var_dump($result);
?>

// output

array(5) {
  [0]=>
  string(6) "zero_a"
  [2]=>
  string(5) "two_a"
  [3]=>
  string(7) "three_a"
  [1]=>
  string(5) "one_b"
  [4]=>
  string(6) "four_b"
}

PHP array_merge vs + array union operator


We already discussed about array_merge in previous section. array_merge – Merge one or more arrays.

Now what is array union operator (+) in array merge operation? Why you need to use it?

+ is Union array Operators in following example, Example $a + $b. Why you need to use array union operator is explained in PHP array merge associative section.

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

PHP array_merge shorthand


PHP array_merge shorthand is + (array union operator). You can use it to merge one or more associative arrays, You can check it out more in PHP array merge associative section.

PHP array merge without changing key


When you use array_merge function to merge associative array. It will merge values and remove keys. In output you will get array without keys.

How to PHP array merge without changing key? Answer is + (array union operator). You can use it to merge one or more associative arrays, You can check it out more in PHP array merge associative section.

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

PHP array_merge preserve keys


PHP array_merge preserve keys, Here is again case for merge associative array and preserve keys.

When you use array_merge function to merge associative array. It will merge values and remove keys. In output you will get array without keys.

How to PHP array_merge preserve keys? Answer is + (array union operator). You can use it to merge one or more associative arrays, You can check it out more in PHP array merge associative section.

PHP merge associative array by key


PHP merge associative array by key, in other words array_merge_recursive — Merge one or more arrays recursively.

array_merge_recursive() recursively merge two arrays recursively, in process it will check all array keys and merge them in one array. You can check it out more in PHP array_merge_recursive section.

PHP merge associative array with duplicate keys


PHP merge associative array with duplicate keys. I tried array_merge function for it but not getting expected result in some cases. Below are some cases where array_merge not return expected result for associative array.

When your array have numeric keys, array_merge consider it as normal index and replace with normal order number, also if your arrays have same keys array_merge() only use one in result output array.

How to PHP merge associative array with duplicate keys? Answer is PHP array_merge_recursive. You can use it to PHP merge associative array with duplicate keys, You can check it out more in PHP array_merge_recursive section.

PHP merge multidimensional associative array


PHP merge multidimensional associative array. PHP array_merge_recursive is one option to merge multidimensional associative array.

PHP array merge Reference sources


Conclusion


Oh… too many process to merge PHP arrays, but we know that it’s not over yet, because based on different logic programmer need to merge generated array different ways.

It may possible some special merge process not comes in above list, but i am sure you will be able to write such process based on current listed PHP array_merge tutorial with examples.

Mostly PHP official document page have all detail and some special case exist in User Contributed Notes, sometime we not check it properly.

Hope you found post helpful, don’t forget to SHARE on social media. also write your queries in comment section, I’ll definitely help you to found best array merge solution for your case.