2014-03-19 145 views
0

我有一个数组,看起来像这样:多维数组合并

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循环来遍历它们,从而根据每个数组中的价格和销售排名在最内层的产品数组中生成一个新元素。

我甚至不知道这是否可能,但任何帮助或甚至推动正确的方向将不胜感激。

感谢,

埃里克

回答

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

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

+0

非常感谢!那正是我需要的! – Eric