Laravel 集合与 diff 相反

问题描述 投票:0回答:2

我有2个收藏:

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

与:

$collection->diff($collection2);

我明白了

[2,3]

我想要的是具有相反的值:

[1,4]

collection中有没有办法或方法可以做到这一点?

php laravel diff intersection laravel-collection
2个回答
2
投票

laravel 集合相交

intersect 方法从原始集合中删除给定数组或集合中不存在的任何值。

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

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

$intersect->all();

1
投票

合并到单个集合后可以使用重复方法

$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,
     ],
   }
© www.soinside.com 2019 - 2024. All rights reserved.