1

我必须组合两个多维数组(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;
}

}

抱歉,我的问题啰嗦了。提前感谢您的帮助。

4

2 回答 2

2

这将递归地合并两个数组并应用回调来确定如何处理匹配键的非数组值。

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;
}

如果键是“值”,回调将把这两个元素相加,否则如果它们相同,它将不理会它,如果它们不相同,它将把它们包装在一个数组中。如果要对任何数值求和,无论键如何,都可以将第一个更改ifif (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);
于 2012-10-05T05:09:41.397 回答
0

节省资源首先使用合并数组

于 2012-10-05T04:02:51.860 回答