如何删除集合中的重复项? [英] How to remove duplicates in collection?
本文介绍了如何删除集合中的重复项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Laravel有收藏品
I have collection in Laravel:
Collection {#450 ▼
#items: array:2 [▼
0 => Announcement {#533 ▶}
1 => Announcement {#553 ▶}
]
}
是相同的项目.如何删除其中之一?
It is the same items. How ti delete one of them?
完整代码是:
public function announcements()
{
$announcements = $this->categories_ann->map(function ($c) {
return $c->announcements->map(function ($a) {
$a->subsribed = true;
return $a;
});
});
$flattened = $announcements->groupBy("id")->flatten();
return $flattened;
}
推荐答案
$unique = $collection->unique();
这篇关于如何删除集合中的重复项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文