我必须组合两个多维数组(4 级深),同时总结每个项目下的数值。除了提到的值之外,这两个数组的结构是相同的。
阵列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
(
)
)
)
)
)
)
和 Array2:
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
(
)
)
)
)
)
)
我的目标是组合这两个数组并对 [value] 数字键求和。到目前为止我的进展:
foreach ($Array2 as $key => $values) {
foreach ($values as $name => $value) {
if(!is_array($Array1[$key])) {
$Array1[$key] = array();
}
$finalArr[$key][$name] += $value;
}
}
抱歉,我的问题啰嗦了。提前感谢您的帮助。