我必須合併兩個多維數組(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;
}
}
對不起,是囉嗦我的問題。預先感謝您的幫助。
他們會永遠在這種結構(即4級深)? – FoolishSeth
是的,結構將保持不變。 – aethon
所以你只想添加從array1到array2的每一對匹配值?所以合併的數組有一樣的東西,但是兩個值的總和? – FoolishSeth