0

我有下面的数组,需要一些额外的结构。

Array
(
[-Others] => Array
    (
        [0] => Array
            (
                [products_sold] => 1
                [products_total_sales] => 2.99
                [products_total_costs] => 1.75
                [products_total_profit] => 1.24
            )

        [1] => Array
            (
                [products_sold] => 1
                [products_total_sales] => 2.3322
                [products_total_costs] => 1.75
                [products_total_profit] => 0.5822
            )

    )

[Addict] => Array
    (
        [0] => Array
            (
                [products_sold] => 1
                [products_total_sales] => 35.1
                [products_total_costs] => 40
                [products_total_profit] => -4.9
            )

很明显,我已经提前截断了。我想知道是否可以将所有子产品的所有销售额加起来,products_total_sales每个品牌(又名addict

我想在它们被解析之前得到它,比如:

[Addict] => Array
    (
        [0] => Array
        [brand_total_sales] => '$value'
            (
                [products_sold] => 1
                ...

任何帮助是极大的赞赏。提前致谢,如果您需要更多信息,请随时询问

4

1 回答 1

1

好吧,如果答案是肯定的:

foreach($array as $brand_name => $brand_array) {
    $array[$brand_name]['brand_total_sales'] = 0;

    foreach($brand_array as $product) {
        $array[$brand_name]['brand_total_sales'] += $product['products_total_sales'];
    }
}
于 2013-10-17T15:47:30.810 回答