简体   繁体   中英

PHP Adding multidimensional arrays together

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

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM