英文:
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]
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论