从数组中分组相同的名称并显示一个独立组的总数

时间:2017-07-24 12:42:10

标签: php html arrays laravel

我有一个看起来像这样的数组:

    Array
    (
        [0] => stdClass Object
            (
                [products_name] => Parla to gold(Flower)
                [measurement] => 18
                [unit] => mm
                [products_size_height] => 0
                [products_size_width] => 0
                [products_size_unit] => inch
                [products_type] => 4
                [products_thickness_measurement] => 18
                [product_ordered_pcs] => 4
                [product_ordered_quantity] => 100
                [others_feature] => 
                [rate] => 500
                [amount] => 50000
            )

        [1] => stdClass Object
            (
                [products_name] => Parla to gold(Flower)
                [measurement] => 18
                [unit] => mm
                [products_size_height] => 0
                [products_size_width] => 0
                [products_size_unit] => inch
                [products_type] => 4
                [products_thickness_measurement] => 18
                [product_ordered_pcs] => 0
                [product_ordered_quantity] => 45
                [others_feature] => 
                [rate] => 45
                [amount] => 2025
            )

        [2] => stdClass Object
            (
                [products_name] => Parla to gold(Flower)
                [measurement] => 18
                [unit] => mm
                [products_size_height] => 0
                [products_size_width] => 0
                [products_size_unit] => inch
                [products_type] => 2
                [products_thickness_measurement] => 18
                [product_ordered_pcs] => 4
                [product_ordered_quantity] => 100
                [others_feature] => 
                [rate] => 850
                [amount] => 85000
            )
.......

现在,我想对同一产品进行分组(产品将通过products_name,measurement和products_type的组合进行识别),以便一次显示一个产品,并总计同一产品的“product_ordered_quantity”。看到您可能理解我想说的图像。我将生成PDF文件,所以没有javascript请。

结果:

enter image description here

我想:

enter image description here

2 个答案:

答案 0 :(得分:1)

您可以尝试这个漂亮的类,它可以让您在查询数据库时查询数组 - https://phplinq.codeplex.com/

答案 1 :(得分:1)

如果这是来自DB的查询,您可以使用Collection类(https://laravel.com/docs/5.4/eloquent-collections)提供的一些方法

r