- 交集: 使用intersect()函数,用于获取两个集合的交集:
$collection1 = collect([1, 2, 3, 4, 5]);
$collection2 = collect([4, 5, 6, 7, 8]);
$intersection = $collection1->intersect($collection2);
- 并集:使用union()函数,用于获取两个集合的并集:
$collection1 = collect([1, 2, 3, 4, 5]);
$collection2 = collect([4, 5, 6, 7, 8]);
$union = $collection1->union($collection2);
- 差集:使用diff()函数,用于获取两个集合的差集:
$collection1 = collect([1, 2, 3, 4, 5]);
$collection2 = collect([4, 5, 6, 7, 8]);
$difference = $collection1->diff($collection2);