我有2个收藏:
$collection = collect([1,2,3,4]);
$collection2 = collect([1,5,6,4]);
与:
$collection->diff($collection2);
我明白了
[2,3]
我想要的是具有相反的值:
[1,4]
collection中有没有办法或方法可以做到这一点?
laravel 集合相交
intersect 方法从原始集合中删除给定数组或集合中不存在的任何值。
$collection = collect([1,2,3,4]);
$collection2 = collect([1,5,6,4]);
$intersect = $collection->intersect($collection2);
$intersect->all();
合并到单个集合后可以使用重复方法
$collection1 = collect([1,2,3,4]);
$collection2 = collect([1,5,6,4]);
$duplicates = $collection1
->merge($collection2)
->duplicates()
->values();
会给你
Illuminate\Support\Collection {#1151
all: [
1,
4,
],
}