使用 Laravel Collection 获取重复值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40888168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use Laravel Collection to get duplicate values
提问by Antonio Morales
I don′t want to remove the duplicate values, I want to get the articles_id duplicates and sum their quantity values, for example, this is my collection:
我不想删除重复值,我想获取articles_id重复项并将它们的数量值相加,例如,这是我的集合:
Collection {#306 ▼
#items: array:3 [▼
0 => CartLine {#294 ▼
+quantity: 2
+article_id: 1728
+article_name: "TAZA CERAMICA"
}
1 => CartLine {#296 ▼
+parent_line_id: null
+quantity: 1
+article_id: 1728
+article_name: "TAZA CERAMICA"
}
2 => CartLine {#298 ▼
+quantity: 1
+article_id: 1378
+article_name: "JARRA CERVEZA ALEMANA"
}
]
}
And I want get this result:
我想得到这个结果:
Collection {#306 ▼
#items: array:3 [▼
0 => CartLine {#294 ▼
+quantity: 3 //sum total quantity of the duplicates elements with same article_id
+article_id: 1728
+article_name: "TAZA CERAMICA"
}
1 => CartLine {#296 ▼
+parent_line_id: null
+quantity: 3
+article_id: 1728
+article_name: "TAZA CERAMICA"
}
2 => CartLine {#298 ▼
+quantity: 1
+article_id: 1378
+article_name: "JARRA CERVEZA ALEMANA"
}
]
}
I want sum the quantities of the duplicate elements and set the quantity property with the sum in these elements.
我想对重复元素的数量求和,并用这些元素中的总和设置数量属性。
回答by Matt Pramschufer
I know this answer was already accepted, but I just found this StackOverflow post when I was searching for the same thing. What I ended up doing was the following.
我知道这个答案已经被接受了,但是我在搜索同样的东西时才发现了这篇 StackOverflow 帖子。我最终做的是以下内容。
$users = Users::all();
$usersUnique = $users->unique('id');
$usersDupes = $users->diff($usersUnique);
dd($users, $usersUnique, $usersDupes);
I only needed mine for a one time purpose so I am not sure how performant this would be if you need to use it on production page.
我只需要我的一次性用途,所以我不确定如果您需要在生产页面上使用它的性能如何。
回答by Rwd
You could try something like:
你可以尝试这样的事情:
$collection->groupBy('article_id')->flatMap(function ($items) {
$quantity = $items->sum('quantity');
return $items->map(function ($item) use ($quantity) {
$item->quantity = $quantity;
return $item;
});
});
Obviously, change $collection
to be whatever you've called the variable that holds your collection.
显然,更改$collection
为您所称的保存集合的变量。
Hope this helps!
希望这可以帮助!
回答by Alexandr Mityuk
Another one solution to obtain grouped duplicates:
获取分组重复项的另一种解决方案:
$collection = collect([
[
'name' => 'aa',
'value' => 22
],
[
'name' => 'bb',
'value' => 22
],
[
'name' => 'cc',
'value' => 11
],
[
'name' => 'bb',
'value' => 33
],
[
'name' => 'bb',
'value' => 33
],
[
'name' => 'bb',
'value' => 33
],
]);
$groupedByValue = $collection->groupBy('value');
$dupes = $groupedByValue->filter(function (Collection $groups) {
return $groups->count() > 1;
});
Output:
输出:
array:2 [▼
22 => array:2 [▼
0 => array:2 [▼
"name" => "aa"
"value" => 22
]
1 => array:2 [▼
"name" => "bb"
"value" => 22
]
]
33 => array:3 [▼
0 => array:2 [▼
"name" => "bb"
"value" => 33
]
1 => array:2 [▼
"name" => "bb"
"value" => 33
]
2 => array:2 [▼
"name" => "bb"
"value" => 33
]
]
]