Laravel从多维数组集合中汇总数值

huangapple go评论67阅读模式
英文:

Laravel sum up values from multidimensional array collection

问题

我有一个类似的集合

$collection = collect([
   [
      ["studentId" => 3, "marks" => 10],
      ["studentId" => 7, "marks" => 15]
   ],
   [
      ["studentId" => 3, "marks" => 20],
      ["studentId" => 7, "marks" => 15]
   ],
   [
      ["studentId" => 3, "marks" => 10],
      ["studentId" => 7, "marks" => 20]
   ],
]);

我想要对特定的studentId求和marks,并映射结果如下

[
   ["studentId" => 3, "marks" => 40],
   ["studentId" => 7, "marks" => 50],
]

谢谢 🙏

英文:

I have a collection like

$collection = collect([
   [
      ["studentId" => 3, "marks" => 10],
      ["studentId" => 7, "marks" => 15]
   ],
   [
      ["studentId" => 3, "marks" => 20],
      ["studentId" => 7, "marks" => 15]
   ],
   [
      ["studentId" => 3, "marks" => 10],
      ["studentId" => 7, "marks" => 20]
   ],
]);

I want to sum up marks for specific studentId and map the results like

[
   ["studentId" => 3, "marks" => 40],
   ["studentId" => 7, "marks" => 50],
]

Thanks 🙏

答案1

得分: 1

以下是代码部分的翻译:

$collection = collect([
   [
      ['studentId' => 3, 'marks' => 10],
      ['studentId' => 7, 'marks' => 15]
   ],
   [
      ['studentId' => 3, 'marks' => 20],
      ['studentId' => 7, 'marks' => 15]
   ],
   [
      ['studentId' => 3, 'marks' => 10],
      ['studentId' => 7, 'marks' => 20]
   ],
]);

$collection = $collection
    ->flatten(1)
    ->groupBy('studentId')
    ->transform(function($subItems, $studentId) {
        return [
            'studentId' => $studentId,
            'marks' => $subItems->sum('marks')
        ];
    })
    ->values();

如果需要对其他部分进行翻译,请提供需要翻译的具体内容。

英文:

This is untested but you can probably do something like...

$collection = collect([
   [
      ['studentId' => 3, 'marks' => 10],
      ['studentId' => 7, 'marks' => 15]
   ],
   [
      ['studentId' => 3, 'marks' => 20],
      ['studentId' => 7, 'marks' => 15]
   ],
   [
      ['studentId' => 3, 'marks' => 10],
      ['studentId' => 7, 'marks' => 20]
   ],
]);

$collection = $collection
    ->flatten(1)
    ->groupBy('studentId')
    ->transform(function($subItems, $studentId) {
        return [
            'studentId' => $studentId,
            'marks' => $subItems->sum('marks')
        ];
    })
    ->values();

What this does is...

1. Flatten the array

First we flatten the collection by 1 depth using the flatten method. Resulting structure...

[
  ['studentId' => 3, 'marks' => 10],
  ['studentId' => 7, 'marks' => 15],
  ['studentId' => 3, 'marks' => 20],
  ['studentId' => 7, 'marks' => 15],
  ['studentId' => 3, 'marks' => 10],
  ['studentId' => 7, 'marks' => 20]
]

2. Group values by

Next we group all the values by the studentId using the groupBy method. Result...

[
  3 => [
    ['studentId' => 3, 'marks' => 10],
    ['studentId' => 3, 'marks' => 20],
    ['studentId' => 3, 'marks' => 10]
  ],
  7 => [
    ['studentId' => 7, 'marks' => 15],
    ['studentId' => 7, 'marks' => 15],
    ['studentId' => 7, 'marks' => 20]
  ]
]

3. Sum the marks

Now we calculate the groups of marks with the transform method. Result...

[
  3 => ['studentId' => 3, 'marks' => 40],
  7 => ['studentId' => 7, 'marks' => 50]
]

4. Reset keys (optional)

You'll notice in the last result that the top level array keys are the same as the studentId values this is left over from the groupBy method. We can reset these with the values method. Result...

[
  ['studentId' => 3, 'marks' => 40],
  ['studentId' => 7, 'marks' => 50]
]

huangapple
  • 本文由 发表于 2023年2月6日 20:05:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75361087.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定