How to combine two multidimensional array in php?

how to join two multidimensional arrays in php? I have two multidimensional arrays A and B. I need to join A and B to form a new array C as follows

$A = array[ 
array["a1"=>1,"b1"=>2,"c1"=>"A"], 
array["a1"=>1,"b1"=>16,"c1"=>"Z"], 
array["a1"=>3,"b1"=>8,"c1"=>"A"]]; 

$B = array[ 
array["a2"=>1,"b2"=>2,"b2"=>"A"], 
array["a2"=>1,"b2"=>16,"b2"=>"G"], 
array["a2"=>3,"b2"=>8,"b2"=>"A"]]; 

//join A and B to form C

$C=array[ 
array["a1"=>1,"b1"=>2,"c1"=>"A"], 
array["a1"=>1,"b1"=>16,"c1"=>"Z"], 
array["a1"=>3,"b1"=>8,"c1"=>"A"],
array["a2"=>1,"b2"=>2,"b2"=>"A"], 
array["a2"=>1,"b2"=>16,"b2"=>"G"], 
array["a2"=>3,"b2"=>8,"b2"=>"A"]];

Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. Start your free 14-day trial today.

If you want to join two multidimensional arrays in PHP, you should still use array_merge, and not array_merge_recursive. Confused? So was I. Let's explain what's happening.

Let's first explain what array_merge_recursive does, take for example these two arrays:

$first = [
    'key' => 'original'
];

$second = [
    'key' => 'override'
];

Using array_merge_recursive will result in the following:

array_merge_recursive[$first, $second];

// [
//     'key' => [
//         'original',
//         'override',
//     ],
// ]

Instead over overriding the original key value, array_merge_recursive created an array, with the original and new value both in it.

While that looks strange in this simple example, it's actually more useful in cases where one of the values already is an array, and you want to merge another item in that array, instead of overriding it.

$first = [
    'key' => ['original']
];

$second = [
    'key' => 'override'
];

In this case, array_merge_recursive will yield the same result as the first example: it takes the value from the $second array, and appends it to the value in the $first array, which already was an array itself.

array_merge_recursive[$first, $second];

// [
//     'key' => [
//         'original',
//         'override',
//     ],
// ]

So if you want to merge multidimensional arrays, you can simply use array_merge, it can handle multiple levels of arrays just fine:

$first = [
    'level 1' => [
        'level 2' => 'original'
    ]
];

$second = [
    'level 1' => [
        'level 2' => 'override'
    ]
];

array_merge[$first, $second];

// [  
//     'level 1' => [
//         'level 2' => 'override'
//     ]
// ]

All of that being said, you could also use the + operator to merge multidimensional arrays, but it will work slightly different compared to array_merge.

Noticed a tpyo? You can submit a PR to fix it. If you want to stay up to date about what's happening on this blog, you can follow me on Twitter or subscribe to my newsletter:

Please check the detailed post about How to merge an array in PHP

To merge arrays, PHP has provided inbuilt functions that will take arrays and will provide final output as a merged array. In this tutorial, we’ll see how to merge an array in PHP.

PHP has provided different functions to group different arrays into single.

  • array_merge[]
  • array_combine[]
  • array_merge_recursive[]

Merge two arrays using array_merge[]

We can merge two or more arrays using array_merge[] and its syntax is quite simple-

array_merge [[ array $... ] ] : array

Here, we can pass as many arrays [as arguments] as possible and the expected output will always be an array.

So, in array_merge[] the resultant array will contain one array appended to another array which means the first array argument will be considered as a parent/reference array to which all the other array elements will be appended.

Chủ Đề