0

I have been wondering how to add 2 multidimensional arrays together, I have found similar solutions but it is not exactly what I am trying to go for. Maybe one of you guys could help me out. Yes I know the title is almost the same as other asked questions, but trust me, I have looked for my answer but I can't find it.

# array1
Array
(
    [0] => Array
        (
            [0] => Product1 
            [1] => Description product 1          
        )

    [1] => Array
        (
            [0] => Product2
            [1] => Description product 2       
        )

    [2] => Array
        (
            [0] => Product3
            [1] => Description product 3       
        )
)

# array2
Array
(
    [0] => Array
        (
            [0] => Price 1
            [1] => Something product 1        
        )

    [1] => Array
        (
            [0] => Price 2
            [1] => Something product 2    
        )

    [2] => Array
        (
            [0] => Price 3
            [1] => Something product 3      
        )
)

#resultant array
Array
(
    [0] => Array
        (
            [0] => Product1 
            [1] => Description product 1
            [3] => Price 1
            [4] => Something product 1
        )

    [1] => Array
        (
            [0] => Product2
            [1] => Description product 2
            [2] => Price 2
            [3] => Something product 2      
        )

    [2] => Array
        (
            [0] => Product3
            [1] => Description product 3  
            [2] => Price 3
            [3] => Something product 3   
        )
)

As you can see I would like to add the 2 arrays together. I have seen several other answers but they use the build in php function array_merge(). If I use that it will result in something like this:

#resultant array
Array
(
    [0] => Array
        (
            [0] => Product1 
            [1] => Description product 1
        )

    [1] => Array
        (
            [0] => Product2
            [1] => Description product 2     
        )

    [2] => Array
        (
            [0] => Product3
            [1] => Description product 3   
        )
    [3] => Array
        (
            [0] => Price 1
            [1] => Something product 1        
        )

    [4] => Array
        (
            [0] => Price 2
            [1] => Something product 2    
        )

    [5] => Array
        (
            [0] => Price 3
            [1] => Something product 3      
        )
)
)

As you can see that is not what I am looking for unfortunately. I am hoping to find a solution for my problem.

Thanks for reading my post.

Cheers Cody

Cody
  • 7
  • 3

2 Answers2

0

You can apply array_merge to each of your subarrays using array_map:

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

For more information check the manual for array_map, especially example 3.

jh1711
  • 2,288
  • 1
  • 12
  • 20
0

you can do this

$final = [];
foreach($arr1 as $key => $value){
// loop over the second array elements
 foreach($arr2[$key] as $key2 => $value2){
 // append the second array values to the first array
   $value[] = $value2;
 }
// append the new array to the final array
$final[] = $value;
}
Islam ElHakmi
  • 274
  • 2
  • 10
  • This seemed to work, but array_map worked better since it is a build in function of PHP and way less code to write, thanks again for your answer. – Cody Jan 11 '19 at 14:22