Laravel collection diff 相反

Laravel collection opposite of diff

我有 2 collections:

$collection = collect([1,2,3,4]);
$collection2 = collect([1,5,6,4]);

与:

$collection->diff($collection2);

我得到 [2,3]

所以我想要的是相反的值,[1,4]

在collection中是否有方法或方法可以做到这一点?

laravel collection intersect

intersect 方法从原始 collection 中删除给定数组或 collection.

中不存在的任何值
$collection = collect([1,2,3,4]);
$collection2 = collect([1,5,6,4]);

$intersect = $collection->intersect($collection2);

$intersect->all();

合并成单个集合后可以使用duplicates方法

$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,
     ],
   }