2012-10-05 53 views
1

我必須合併兩個多維數組(4級深),同時總結每個項目下的數值。兩個數組的結構與上述數值不同。結合兩個多維數組和總計只有一個關鍵瓦特/數值 - PHP

數組1:

Array 
(
    [0] => Array 
     (
      [item_code] => AB00 
      [item_text] => Alcoholic beverages 
      [value] => 233 
      [children] => Array 
       (
       ) 

     ) 

    [1] => Array 
     (
      [item_code] => AP00 
      [item_text] => Apparel and services 
      [value] => 1080 
      [children] => Array 
       (
        [0] => Array 
         (
          [item_code] => APC0 
          [item_text] => Apparel, Children under 2 
          [value] => 79 
          [children] => Array 
           (
           ) 

         ) 

        [1] => Array 
         (
          [item_code] => APO0 
          [item_text] => Other apparel products and services 
          [value] => 131 
          [children] => Array 
           (
           ) 

         ) 

        [2] => Array 
         (
          [item_code] => APM0 
          [item_text] => Apparel, Men and boys 
          [value] => 204 
          [children] => Array 
           (
            [0] => Array 
             (
              [item_code] => APM2 
              [item_text] => Apparel, Boys, 2 to 15 
              [value] => 41 
              [children] => Array 
               (
               ) 

             ) 

            [1] => Array 
             (
              [item_code] => APM1 
              [item_text] => Apparel, Men, 16 and over 
              [value] => 163 
              [children] => Array 
               (
               ) 

             ) 

           ) 

         ) 

        [3] => Array 
         (
          [item_code] => APF0 
          [item_text] => Footwear 
          [value] => 241 
          [children] => Array 
           (
           ) 

         ) 

        [4] => Array 
         (
          [item_code] => APW0 
          [item_text] => Apparel, Women and girls 
          [value] => 425 
          [children] => Array 
           (
            [0] => Array 
             (
              [item_code] => APW2 
              [item_text] => Apparel, Girls, 2 to 15 
              [value] => 78 
              [children] => Array 
               (
               ) 

             ) 

            [1] => Array 
             (
              [item_code] => APW1 
              [item_text] => Apparel, Women, 16 and over 
              [value] => 347 
              [children] => Array 
               (
               ) 

             ) 

           ) 

         ) 

       ) 

     ) 

和數組2:

Array 
(
    [0] => Array 
     (
      [item_code] => AB00 
      [item_text] => Alcoholic beverages 
      [value] => 296 
      [children] => Array 
       (
       ) 

     ) 

    [1] => Array 
     (
      [item_code] => AP00 
      [item_text] => Apparel and services 
      [value] => 1225 
      [children] => Array 
       (
        [0] => Array 
         (
          [item_code] => APC0 
          [item_text] => Apparel, Children under 2 
          [value] => 73 
          [children] => Array 
           (
           ) 

         ) 

        [1] => Array 
         (
          [item_code] => APO0 
          [item_text] => Other apparel products and services 
          [value] => 163 
          [children] => Array 
           (
           ) 

         ) 

        [2] => Array 
         (
          [item_code] => APF0 
          [item_text] => Footwear 
          [value] => 265 
          [children] => Array 
           (
           ) 

         ) 

        [3] => Array 
         (
          [item_code] => APM0 
          [item_text] => Apparel, Men and boys 
          [value] => 278 
          [children] => Array 
           (
            [0] => Array 
             (
              [item_code] => APM2 
              [item_text] => Apparel, Boys, 2 to 15 
              [value] => 61 
              [children] => Array 
               (
               ) 

             ) 

            [1] => Array 
             (
              [item_code] => APM1 
              [item_text] => Apparel, Men, 16 and over 
              [value] => 217 
              [children] => Array 
               (
               ) 

             ) 

           ) 

         ) 

        [4] => Array 
         (
          [item_code] => APW0 
          [item_text] => Apparel, Women and girls 
          [value] => 447 
          [children] => Array 
           (
            [0] => Array 
             (
              [item_code] => APW2 
              [item_text] => Apparel, Girls, 2 to 15 
              [value] => 95 
              [children] => Array 
               (
               ) 

             ) 

            [1] => Array 
             (
              [item_code] => APW1 
              [item_text] => Apparel, Women, 16 and over 
              [value] => 353 
              [children] => Array 
               (
               ) 

             ) 

           ) 

         ) 

       ) 

     ) 

我的目標是這兩個數組結合起來,總結了[值]數字鍵。到目前爲止,我的進步:

foreach ($Array2 as $key => $values) { 
foreach ($values as $name => $value) { 
    if(!is_array($Array1[$key])) { 
     $Array1[$key] = array(); 
    } 
    $finalArr[$key][$name] += $value; 
} 

}

對不起,是囉嗦我的問題。預先感謝您的幫助。

+0

他們會永遠在這種結構(即4級深)? – FoolishSeth

+0

是的,結構將保持不變。 – aethon

+0

所以你只想添加從array1到array2的每一對匹配值?所以合併的數組有一樣的東西,但是兩個值的總和? – FoolishSeth

回答

1

這將遞歸合併兩個數組並應用回調,以確定如何處理匹配鍵非數組值做。

function array_merge_map_recursive($r1, $r2, $callback) { 
    $merged = array(); 
    foreach ($r1 as $k => $v) { 
     if (!isset($r2[$k])) { 
      $merged[$k] = $v; 
     } 
     elseif (!is_array($v)) { 
      $merged[$k] = $callback($k,$v,$r2[$k]); 
     } 
     else { 
      $merged[$k] = array_merge_map_recursive($v,$r2[$k],$callback); 
     } 
    } 
    return $merged; 
} 

回調將總結這兩個元素,如果關鍵是「價值」,否則的話他們會離開它單獨時相同的,如果他們不一樣,將他們包裹在一個數組。如果你想總結任何數值,不管鍵是什麼,你可以將第一個if更改爲if (is_numeric($v1))

該語法需要PHP 5.3,但它可以很容易地重寫爲早期版本。

$sumIfValue = function($key,$v1,$v2) { 
    if ($key==='value') { 
     return $v1 + $v2; 
    } elseif ($v1===$v2) { 
     return $v1; 
    } else { 
     return array($v1,$v2); 
    } 
}; 

然後,你可以這樣調用(如您的兩個數組$array1$array2):

$merged = array_merge_map_recursive($array1, $array2, $sumIfValue); 
+0

我從原來寫的那一行改正了它,做我認爲你在尋找的東西。嘗試運行它並var_dump'out輸出以確保你得到你所期望的。 – FoolishSeth

+0

感謝百萬++ @FoolishSeth,這工作完美。 – aethon