我有10个(3用于下面的示例)集合我需要通过键合并在一起,但需要对值进行求和。我的收藏品看起来像这样:
[
'key1' => ['value1' => 5, 'value2' => 3, 'value3' => 0],
'key2' => ['value1' => 1, 'value2' => 6, 'value3' => 2],
'key3' => ['value1' => 0, 'value2' => 0, 'value3' => 1],
]
[
'key1' => ['value1' => 3, 'value2' => 1, 'value3' => 7],
'key2' => ['value1' => 1, 'value2' => 3, 'value3' => 2],
'key3' => ['value1' => 1, 'value2' => 6, 'value3' => 1],
]
[
'key1' => ['value1' => 2, 'value2' => 3, 'value3' => 9],
'key2' => ['value1' => 3, 'value2' => 8, 'value3' => 3],
'key3' => ['value1' => 1, 'value2' => 0, 'value3' => 6],
]
我希望得到一个结果如下所示的集合:
[
'key1' => ['value1' => 10, 'value2' => 7, 'value3' => 16],
'key2' => ['value1' => 5, 'value2' => 17, 'value3' => 7],
'key3' => ['value1' => 2, 'value2' => 6, 'value3' => 8],
]
我是否缺少内置的Laravel Collection方法或者实现此目的的最佳方法是什么?
编辑(更多信息)值始终为数字。任何集合中都没有字符串值。
edit2:有些人在问我尝试过什么。我试过这个有效:
$merged = [];
foreach ($seeds as $seed) {
foreach ($seed as $location => $values) {
foreach ($values as $key => $value) {
$merged[$location][$key] = $value + ($merged[$location][$key] ?? 0);
}
}
}
我只是想知道是否有更简洁的方法来实现同样的事情,而不是使用3个foreach循环。
答案 0 :(得分:0)
真的以为会有比循环更容易做的事情,但似乎解决方案仍然比我想出的更好。即使更改深度,Flatten也会移除键,mapToGroups仅在返回包含单个键/值对的关联数组时有效,pluck会带来一些其他困难等等。
这就是我想出来的
$test = collect([
[
'key1' => ['value1' => 5, 'value2' => 3, 'value3' => 0],
'key2' => ['value1' => 1, 'value2' => 6, 'value3' => 2],
'key3' => ['value1' => 0, 'value2' => 0, 'value3' => 1],
],
[
'key1' => ['value1' => 3, 'value2' => 1, 'value3' => 7],
'key2' => ['value1' => 1, 'value2' => 3, 'value3' => 2],
'key3' => ['value1' => 1, 'value2' => 6, 'value3' => 1],
],
[
'key1' => ['value1' => 2, 'value2' => 3, 'value3' => 9],
'key2' => ['value1' => 3, 'value2' => 8, 'value3' => 3],
'key3' => ['value1' => 1, 'value2' => 0, 'value3' => 6],
]
]);
$mappedCollection = collect($test->first())->keys()->mapWithKeys(function($item,$key) use($test){
return[
$item => $test->map(function ($mapItem, $mapKey) use($item) {
return $mapItem[$item];
})
];
})->mapWithKeys(function($item,$key){
$eachLine = collect($item->first())->keys()->mapWithKeys(function($mapItem) use($item){
return[ $mapItem => $item->sum($mapItem) ];
});
return [$key => $eachLine];
})->all();
dd($mappedCollection);
答案 1 :(得分:0)
我用laravel 7做到了这一点,以防它对某人有用
$test = collect([
[
'key1' => ['value1' => 5, 'value2' => 3, 'value3' => 0],
'key2' => ['value1' => 1, 'value2' => 6, 'value3' => 2],
'key3' => ['value1' => 0, 'value2' => 0, 'value3' => 1],
],
[
'key1' => ['value1' => 3, 'value2' => 1, 'value3' => 7],
'key2' => ['value1' => 1, 'value2' => 3, 'value3' => 2],
'key3' => ['value1' => 1, 'value2' => 6, 'value3' => 1],
],
[
'key1' => ['value1' => 2, 'value2' => 3, 'value3' => 9],
'key2' => ['value1' => 3, 'value2' => 8, 'value3' => 3],
'key3' => ['value1' => 1, 'value2' => 0, 'value3' => 6],
]
]);
$col = $test->reduce(function ($carry, $item) {
return $carry->mergeRecursive($item);
}, collect([]));
$col->transform(function($item) {
return collect($item)->map(function ($item2) {
return collect($item2)->sum();
});
});