phparraysmultidimensional-arraymerge

Merge items from two multidimensional arrays related by first level indexes


I have two arrays..Array1 and Array 2. Both the array contain 2 records.

Now i need to merge both the array inside one array.

In my second array I am getting below two fields from u_data.

[totalcharge] => 15
[RoadTotal] => 15

Now I need to merge these fields in first array.

I need to merge multiple arrays. Below is the problem:

I have tried something like this...but I cannot achieve my expected output.

$abc = array_merge($content, $modecost);

Array 1

Array
(
    [0] => Array
        (
            [u_data] => Array
                (
                    [Mode] => Rail
                    [TotalCost] => 150
                )

            [0] => Array
                (
                    [CostSavingRoutes] => 1
                    [RailCostSaving] => 150
                    [Costsavingshipments] => 2
                )

            [origin_city] => Array
                (
                    [origin_pcode] => 2046
                )

            [dest_city] => Array
                (
                    [dest_pcode] => 4361
                )

        )

    [1] => Array
        (
            [u_data] => Array
                (
                    [Mode] => Rail
                    [TotalCost] => 150
                )

            [0] => Array
                (
                    [CostSavingRoutes] => 1
                    [RailCostSaving] => 150
                    [Costsavingshipments] => 2
                )

            [origin_city] => Array
                (
                    [origin_pcode] => 3170
                )

            [dest_city] => Array
                (
                    [dest_pcode] => 4670
                )

        )

)

Array 2

Array
(
    [0] => Array
        (
            [u_data] => Array
                (
                    [totalcharge] => 15
                    [RoadTotal] => 15
                )

        )

    [1] => Array
        (
            [u_data] => Array
                (
                    [totalcharge] => 15
                    [RoadTotal] => 15
                )

        )

)

Ultimately I would like it to look like this

Array
(
    [0] => Array
        (
            [u_data] => Array
                (
                    [Mode] => Rail
                    [TotalCost] => 150
                    [totalcharge] => 15
                    [RoadTotal] => 15
                )

            [0] => Array
                (
                    [CostSavingRoutes] => 1
                    [RailCostSaving] => 150
                    [Costsavingshipments] => 2
                )

            [origin_city] => Array
                (
                    [origin_pcode] => 2046
                )

            [dest_city] => Array
                (
                    [dest_pcode] => 4361
                )

        )

    [1] => Array
        (
            [u_data] => Array
                (
                    [Mode] => Rail
                    [TotalCost] => 150
            [totalcharge] => 15
                    [RoadTotal] => 15
                )

            [0] => Array
                (
                    [CostSavingRoutes] => 1
                    [RailCostSaving] => 150
                    [Costsavingshipments] => 2
                )

            [origin_city] => Array
                (
                    [origin_pcode] => 3170
                )

            [dest_city] => Array
                (
                    [dest_pcode] => 4670
                )

        )

)

Output of array_merge_recursive().

I try below code but getting different result as I want...

$array3 = array_merge_recursive($content, $modecost);         
echo "<pre>";
print_r($array3);
exit();

I want all the u_data fields inside one array, not in adifferent array.

Array
(
    [0] => Array
        (
            [u_data] => Array
                (
                    [Mode] => Rail
                    [TotalCost] => 150
                )

            [0] => Array
                (
                    [CostSavingRoutes] => 1
                    [RailCostSaving] => 150
                    [Costsavingshipments] => 2
                )

            [origin_city] => Array
                (
                    [origin_pcode] => 2046
                )

            [dest_city] => Array
                (
                    [dest_pcode] => 4361
                )

        )

    [1] => Array
        (
            [u_data] => Array
                (
                    [Mode] => Rail
                    [TotalCost] => 150
                )

            [0] => Array
                (
                    [CostSavingRoutes] => 1
                    [RailCostSaving] => 150
                    [Costsavingshipments] => 2
                )

            [origin_city] => Array
                (
                    [origin_pcode] => 3170
                )

            [dest_city] => Array
                (
                    [dest_pcode] => 4670
                )

        )

    [2] => Array
        (
            [u_data] => Array
                (
                    [totalcharge] => 15
                    [RoadTotal] => 15
                )

        )

    [3] => Array
        (
            [u_data] => Array
                (
                    [totalcharge] => 15
                    [RoadTotal] => 15
                )

        )

)

Solution

  • Use array_merge_recursive in proper way.

    $arr1 = array(
                '0' => array(
                    'u_data' => array(
                        'Mode' => 'Rail',
                        'TotalCost' => 150,),
                    '0' => array(
                        'CostSavingRoutes' => 1,
                        'RailCostSaving' => 150,
                        'Costsavingshipments' => 2,),
                    'origin_city' => array(
                        'origin_pcode' => 2046,),
                    'dest_city' => array(
                        'dest_pcode' => 4361,),
                ),
                '1' => array(
                    'u_data' => array(
                        'Mode' => 'Rail',
                        'TotalCost' => 150,),
                    '0' => array(
                        'CostSavingRoutes' => 1,
                        'RailCostSaving' => 150,
                        'Costsavingshipments' => 2,),
                    'origin_city' => array(
                        'origin_pcode' => 3170,),
                    'dest_city' => array(
                        'dest_pcode' => 4670,),
                ),
    );
    $arr2 =array(
            '0' => array(
                'u_data' => array(
                    'totalcharge' => 15,
                    'RoadTotal' => 15,)
            ),
            '1' => array(
                'u_data' => array(
                    'totalcharge' => 15,
                    'RoadTotal' => 15,),
            )
    );
    
    for ($i=0; $i < count($arr1); $i++) { 
        $final[] = array_merge_recursive($arr1[$i], $arr2[$i]);    
    }
    
    
    echo "<pre>".print_r($arr1,1)."</pre>";
    echo "<pre>".print_r($arr2,1)."</pre>";
    echo "<pre> Final  ---> ".print_r($final,1)."</pre>";