多维数组合并

时间:2014-03-19 15:25:05

标签: php arrays multidimensional-array

我有一个如下所示的数组:

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
                        )

                )

        )

)

也许我会以错误的方式解决这个问题,但我正在尝试合并索引为[0]和[1]的最外层数组。数组是从API查询生成的。最外层的数组索引可以无限期地继续。我正在尝试将它们组合在一起,这样我就可以使用for循环遍历它们,根据每个数组中的价格和销售排名,在最内部的产品数组中生成一个新元素。

我甚至不知道这是否可行,但是我们将非常感谢任何帮助甚至是正确方向的推动。

谢谢,

埃里克

1 个答案:

答案 0 :(得分:2)

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

现在$result应该是一个关联数组,其中包含所有原始子数组的合并内容。