多维数组PHP中的聚合值

问题描述 投票:-2回答:1

从一开始,我就以以下方式获得订单清单:

                (
                    [product_id] => 1094
                    [total_weight] => 0.0390
                    [qty_ordered] => 10.0000
                    [category_id] => 12
                )

我想要的是基于category_idtotal_weightqty_ordered的值求和

我现在已经成功地使用foreach以以下方式获取了数组。但是我需要以某种方式汇总这些值。

Array
(
    [14] => Array
        (
            [0] => Array
                (
                    [product_id] => 1099
                    [total_weight] => 0.0390
                    [qty_ordered] => 10.0000
                )
            [1] => Array
                (
                    [product_id] => 1095
                    [total_weight] => 0.0395
                    [qty_ordered] => 10.0000
                )
        )
    [12] => Array
        (
            [0] => Array
                (
                    [product_id] => 1094
                    [total_weight] => 0.0390
                    [qty_ordered] => 10.0000
                )
        )
    [15] => Array
        (
            [0] => Array
                (
                    [product_id] => 742
                    [total_weight] => 0.1250
                    [qty_ordered] => 1.0000
                )
        )
)

所以理想情况下,我的输出将与下面的一样,并且代码尽可能少:

Array
(
    [14] => Array
        (
            [total_weight] => 0.0785
            [qty_ordered] => 20.0000
        )

    [12] => Array
        (
            [total_weight] => 0.0390
            [qty_ordered] => 10.0000
        )

    [15] => Array
        (
            [total_weight] => 0.1250
            [qty_ordered] => 1.0000
        )
)

[从一开始,我就以以下方式获得订单清单:([product_id] => 1094 [total_weight] => 0.0390 [qty_ordered] ...

php arrays multidimensional-array
1个回答
1
投票

我可以按以下方式对其进行排序。

© www.soinside.com 2019 - 2024. All rights reserved.