phparraysmultidimensional-arraymerging-data

Merge two multidimensional arrays related by shared subarray column values


I have two array Array1 and Array2 and now i want to combine or you can say merge these array into one according to product_option_id in each array means if product_option_id is same then push the second array data into one.

my First array is :

Array
(
    [0] => Array
        (
            [DispensaryInventory] => Array
                (
                    [id] => 21
                    [dispensary_id] => 18
                    [product_id] => 7
                    [product_option_id] => 1

                )


        )

    [1] => Array
        (
            [DispensaryInventory] => Array
                (
                    [id] => 22
                    [dispensary_id] => 18
                    [product_id] => 7
                    [product_option_id] => 2

                )

        )

    [2] => Array
        (
            [DispensaryInventory] => Array
                (
                    [id] => 23
                    [dispensary_id] => 18
                    [product_id] => 7
                    [product_option_id] => 3

                )

        )

    [3] => Array
        (
            [DispensaryInventory] => Array
                (
                    [id] => 24
                    [dispensary_id] => 18
                    [product_id] => 7
                    [product_option_id] => 4

                )

        )

    [4] => Array
        (
            [DispensaryInventory] => Array
                (
                    [id] => 25
                    [dispensary_id] => 18
                    [product_id] => 7
                    [product_option_id] => 5

                )

        )

)

and second array is

Array
(
    [0] => Array
        (
            [DriverInventory] => Array
                (
                    [id] => 21
                    [driver_id] => 10
                    [dispensary_inventory_id] => 12
                    [product_id] => 7
                    [product_option_id] => 2

                )
            [Mydata] => Array
            (
                [price]= 2545
            )    

        )

    [1] => Array
        (
            [DriverInventory] => Array
                (
                    [id] => 22
                    [driver_id] => 10
                    [dispensary_inventory_id] => 13
                    [product_id] => 7
                    [product_option_id] => 3
                    [quantity] => 16

                )
             [Mydata] => Array
                (
                    [price]= 15987
                )    


        )

    [2] => Array
        (
            [DriverInventory] => Array
                (
                    [id] => 23
                    [driver_id] => 10
                    [dispensary_inventory_id] => 14
                    [product_id] => 7
                    [product_option_id] => 4
                    [quantity] => 20

                )

             [Mydata] => Array
                (
                    [price]= 96744
                )    



        )

    [3] => Array
        (
            [DriverInventory] => Array
                (
                    [id] => 24
                    [driver_id] => 10
                    [dispensary_inventory_id] => 15
                    [product_id] => 7
                    [product_option_id] => 5
                    [quantity] => 45

                )
            [Mydata] => Array
                (
                    [price]= 97455
                )    


        )

)

finally i want to be a result as

Array
(
    [0] => Array
        (
            [DispensaryInventory] => Array
                (
                    [id] => 21
                    [dispensary_id] => 18
                    [product_id] => 7
                    [product_option_id] => 1

                )



        )

    [1] => Array
        (
            [DispensaryInventory] => Array
                (
                    [id] => 22
                    [dispensary_id] => 18
                    [product_id] => 7
                    [product_option_id] => 2

                )
            [Mydata] => Array
            (
                [price]= 2545
            )       

        )

    [2] => Array
        (
            [DispensaryInventory] => Array
                (
                    [id] => 23
                    [dispensary_id] => 18
                    [product_id] => 7
                    [product_option_id] => 3

                )
            [Mydata] => Array
                (
                    [price]= 15987
                )       

        )

    [3] => Array
        (
            [DispensaryInventory] => Array
                (
                    [id] => 24
                    [dispensary_id] => 18
                    [product_id] => 7
                    [product_option_id] => 4

                )
            [Mydata] => Array
                (
                    [price]= 96744
                )      

        )

    [4] => Array
        (
            [DispensaryInventory] => Array
                (
                    [id] => 25
                    [dispensary_id] => 18
                    [product_id] => 7
                    [product_option_id] => 5

                )
             [Mydata] => Array
                (
                    [price]= 97455
                )       

        )

)

Solution

  • Try this.

    foreach ($array1 as $key => $value) 
    {
       foreach ($array2 as $key1 => $value1) 
        {
           if($value['DispensaryInventory']['product_option_id']==$value1['DriverInventory']['product_option_id'])
                {
                    $price_data[$key]['Mydata'] = $value1['Mydata'];
                }
        }
    }