Eric
Eric

Reputation: 21

Multidimensional Array Merging

I have an array that looks like this:

Array
(
    [0] => Array
        (
            [ASINS] => Array
                (
                    [0] => B0000714BR
                    [1] => B0002F6BT2
                    [2] => B000Y11B7G
                    [3] => B00571IMWK
                    [4] => 0767827716
                )

            [Product] => Array
                (
                    [0] => Array
                        (
                            [Title] => The Best Little Whorehouse in Texas
                            [UPC] => 025192022029
                            [ASIN] => B0000714BR
                            [SalesRank] => 13104
                            [ImageURL] => http://ecx.images-amazon.com/images/I/51E0DAT5E8L._SL200_.jpg
                            [ProductGroup] => DVD
                            [Publisher] => Universal Studios
                            [NumberOfDiscs] => 1
                            [LowestPrice] => 2.91
                            [ShippingPrice] => 3.99
                            [TotalPrice] => 6.9
                        )

                    [1] => Array
                        (
                            [Title] => Are You Being Served? Again! (The Complete Series)
                            [UPC] => 794051202420
                            [ASIN] => B0002F6BT2
                            [SalesRank] => 13118
                            [ImageURL] => http://ecx.images-amazon.com/images/I/61JAD7WAT0L._SL200_.jpg
                            [ProductGroup] => DVD
                            [Publisher] => BBC Home Entertainment
                            [NumberOfDiscs] => 2
                            [LowestPrice] => 10.99
                            [ShippingPrice] => 3.99
                            [TotalPrice] => 14.98
                        )

                    [2] => Array
                        (
                            [Title] => Homeward Bound - The Incredible Journey / Homeward Bound II - Lost In San Francisco
                            [UPC] => 786936758320
                            [ASIN] => B000Y11B7G
                            [SalesRank] => 2145
                            [ImageURL] => http://ecx.images-amazon.com/images/I/61bMYabUQkL._SL200_.jpg
                            [ProductGroup] => DVD
                            [Publisher] => Walt Disney Home Entertainment
                            [NumberOfDiscs] => 2
                            [LowestPrice] => 19.95
                            [ShippingPrice] => 0.00
                            [TotalPrice] => 19.95
                        )

                    [3] => Array
                        (
                            [Title] => Joy Ride / Joy Ride 2: Dead Ahead (Two-Pack)
                            [UPC] => 024543647058
                            [ASIN] => B00571IMWK
                            [SalesRank] => 113077
                            [ImageURL] => http://ecx.images-amazon.com/images/I/5169VAQVGZL._SL200_.jpg
                            [ProductGroup] => DVD
                            [Publisher] => 20th Century Fox
                            [NumberOfDiscs] => 2
                            [LowestPrice] => 6.49
                            [ShippingPrice] => 3.99
                            [TotalPrice] => 10.48
                        )

                    [4] => Array
                        (
                            [Title] => Christine
                            [UPC] => 043396016194
                            [ASIN] => 0767827716
                            [SalesRank] => 40140
                            [ImageURL] => http://ecx.images-amazon.com/images/I/510A99AQWHL._SL200_.jpg
                            [ProductGroup] => DVD
                            [Publisher] => Sony Pictures Home Entertainment
                            [NumberOfDiscs] => 1
                            [LowestPrice] => 2.81
                            [ShippingPrice] => 3.99
                            [TotalPrice] => 6.8
                        )

                )

        )

    [1] => Array
        (
            [ASINS] => Array
                (
                    [0] => B00005JPH2
                    [1] => B00006JMRE
                    [2] => B000U1ZV7G
                    [3] => B00004RJ71
                    [4] => B00008977G
                )

            [Product] => Array
                (
                    [0] => Array
                        (
                            [Title] => The Chronicles of Narnia: Prince Caspian
                            [UPC] => 786936735437
                            [ASIN] => B00005JPH2
                            [SalesRank] => 2848
                            [ImageURL] => http://ecx.images-amazon.com/images/I/618uTYKkadL._SL200_.jpg
                            [ProductGroup] => DVD
                            [Publisher] => Walt Disney Studios Home Entertainment
                            [NumberOfDiscs] => 1
                            [LowestPrice] => 6.62
                            [ShippingPrice] => 0.00
                            [TotalPrice] => 6.62
                        )

                    [1] => Array
                        (
                            [Title] => High Noon (Collector's Edition)
                            [UPC] => 017153125719
                            [ASIN] => B00006JMRE
                            [SalesRank] => 99262
                            [ImageURL] => http://ecx.images-amazon.com/images/I/51fPYWpMqaL._SL200_.jpg
                            [ProductGroup] => DVD
                            [Publisher] => Republic Pictures
                            [NumberOfDiscs] => 1
                            [LowestPrice] => 3.26
                            [ShippingPrice] => 3.99
                            [TotalPrice] => 7.25
                        )

                    [2] => Array
                        (
                            [Title] => 4 Film Favorites: Draculas (Dracula A.D. 1972, Dracula Has Risen from the Grave, Horror of Dracula, Taste the Blood of Dracula)
                            [UPC] => 085391174264
                            [ASIN] => B000U1ZV7G
                            [SalesRank] => 14994
                            [ImageURL] => http://ecx.images-amazon.com/images/I/61CBLkwawNL._SL200_.jpg
                            [ProductGroup] => DVD
                            [Publisher] => Warner Home Video
                            [NumberOfDiscs] => 2
                            [LowestPrice] => 6.30
                            [ShippingPrice] => 0.00
                            [TotalPrice] => 6.3
                        )

                    [3] => Array
                        (
                            [Title] => Blood In, Blood Out
                            [UPC] => 717951008701
                            [ASIN] => B00004RJ71
                            [SalesRank] => 1862
                            [ImageURL] => http://ecx.images-amazon.com/images/I/51N7R17P51L._SL200_.jpg
                            [ProductGroup] => DVD
                            [Publisher] => Hollywood Pictures Home Entertainment
                            [NumberOfDiscs] => 1
                            [LowestPrice] => 1.37
                            [ShippingPrice] => 3.99
                            [TotalPrice] => 5.36
                        )

                    [4] => Array
                        (
                            [Title] => Gone Fishin'
                            [UPC] => 786936182576
                            [ASIN] => B00008977G
                            [SalesRank] => 13628
                            [ImageURL] => http://ecx.images-amazon.com/images/I/51XQ9YR8BML._SL200_.jpg
                            [ProductGroup] => DVD
                            [Publisher] => Walt Disney Video// Mill Creek
                            [NumberOfDiscs] => 1
                            [LowestPrice] => 3.96
                            [ShippingPrice] => 3.99
                            [TotalPrice] => 7.95
                        )

                )

        )

)

Maybe I am going about this the wrong way, but I am trying to merge the outer most arrays indexed [0] and [1]. The arrays are produced from an API query. The outer most arrays indexes could continue indefinitely. I am trying to combine them so I can loop through them using a for loop to produce a new element in the inner most product arrays based on the price and sales rank in each array.

I do not even know if this is possible but any help or even a push in the right direction would be greatly appreciated.

Thanks,

Eric

Upvotes: 0

Views: 51

Answers (1)

Barmar
Barmar

Reputation: 782785

$result = $array[0];
for ($i = 1; $i < count($array); $i++) {
    foreach ($result as $key => &$value) {
        $value = array_merge($value, $array[$i][$key]);
    }
}

Now $result should be an associative array that contains the merged contents of all the original sub-arrays.

Upvotes: 2

Related Questions