【问题标题】:Laravel Lumen Collection - Group By With Sum and preserve the valuesLaravel Lumen Collection - 按总和分组并保留值
【发布时间】:2021-11-17 18:31:01
【问题描述】:

我正在研究 Lumen,我的收藏有重复的值,例如:

collect([
 [
        'name' => 'John Doe',
        'department' => 'Sales',
        'phone' => '99999-99999',
        'value' => 25.0
    ],
    [
        'name' => 'Mary Lisa',
        'department' => 'Finance',
        'phone' => '88888-88888',
        'value' => 5.0
    ],
    [
        'name' => 'Mary Lisa',
        'department' => 'Finance',
        'phone' => '88888-88888',
        'value' => 58.0
    ],
    [
        'name' => 'Lucas Rodrigues',
        'department' => 'Marketing',
        'phone' => '22222-22222',
        'value' => 90.0
    ]
])

当名称相同但保留其他值(例如(部门和电话))并删除重复实体时,我想对 value 属性求和,例如:

collect([
 [
        'name' => 'John Doe',
        'department' => 'Sales',
        'phone' => '99999-99999',
        'value' => 25.0
    ],
    [
        'name' => 'Mary Lisa',
        'department' => 'Finance',
        'phone' => '88888-88888',
        'value' => 63.0
    ],
    [
        'name' => 'Lucas Rodrigues',
        'department' => 'Marketing',
        'phone' => '22222-22222',
        'value' => 90.0
    ]
])

最好的方法是什么?

【问题讨论】:

    标签: php laravel lumen


    【解决方案1】:

    这可以使用集合函数 (https://laravel.com/docs/8.x/collections) 来实现。在这个例子中,我使用(groupBymapflattenslicecountsum)解决了这个问题。

    $data = collect([
        [
            'name' => 'John Doe',
            'department' => 'Sales',
            'phone' => '99999-99999',
            'value' => 25.0
        ],
        [
            'name' => 'Mary Lisa',
            'department' => 'Finance',
            'phone' => '88888-88888',
            'value' => 5.0
        ],
        [
            'name' => 'Mary Lisa',
            'department' => 'Finance',
            'phone' => '88888-88888',
            'value' => 58.0
        ],
        [
            'name' => 'Lucas Rodrigues',
            'department' => 'Marketing',
            'phone' => '22222-22222',
            'value' => 90.0
        ]
    ]);
    
    $data = $data->groupBy('name')->map(function ($item) {
        if ($item->count() > 1) {
            $item = $item->slice(0, 1)->map(function($subItem) use ($item) {
                $subItem['value'] = $item->sum('value');
    
                return $subItem;
            });
        }
    
        return $item;
    })
    ->flatten(1);
    

    当调用print_r($data->toArray()); 时,我们得到以下数组作为结果:

    Array
    (
        [0] => Array
            (
                [name] => John Doe
                [department] => Sales
                [phone] => 99999-99999
                [value] => 25
            )
    
        [1] => Array
            (
                [name] => Mary Lisa
                [department] => Finance
                [phone] => 88888-88888
                [value] => 63
            )
    
        [2] => Array
            (
                [name] => Lucas Rodrigues
                [department] => Marketing
                [phone] => 22222-22222
                [value] => 90
            )
    
    )
    

    【讨论】:

    • 不错!谢谢;)
    【解决方案2】:

    尝试以下方法:

    $newCollection = collect([]);
    $collectctions = collect([
        [
            'name' => 'John Doe',
            'department' => 'Sales',
            'phone' => '99999-99999',
            'value' => 25.0
        ],
        [
            'name' => 'Mary Lisa',
            'department' => 'Finance',
            'phone' => '88888-88888',
            'value' => 5.0
        ],
        [
            'name' => 'Mary Lisa',
            'department' => 'Finance',
            'phone' => '88888-88888',
            'value' => 58.0
        ],
        [
            'name' => 'Lucas Rodrigues',
            'department' => 'Marketing',
            'phone' => '22222-22222',
            'value' => 90.0
        ]
    ]);
    
    foreach ($collectctions as $item) {
        if($newCollection->contains('name', $item['name'])){
            $index = $newCollection->where('name', $item['name'])->keys()[0];
    
            $newCollection = $newCollection->map(function ($object, $i) use ($index, $item) {
                if($i == $index){
                    $object['value'] += $item['value'];
                }
    
                return $object;
            });
    
        }
        else{
            $newCollection->push($item);
        }
    }
    
    return $newCollection;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      相关资源
      最近更新 更多