i have 2 collections:
$collection = collect([1,2,3,4]);
$collection2 = collect([1,5,6,4]);
with:
$collection->diff($collection2);
i get [2,3]
so what i want is to have the opposite value, [1,4]
Is there a way or method in collection to make this?
CodePudding user response:
You can use duplicates method after mergging into single collection
$collection1 = collect([1,2,3,4]);
$collection2 = collect([1,5,6,4]);
$duplicates = $collection1
->merge($collection2)
->duplicates()
->values();
Will give you
Illuminate\Support\Collection {#1151
all: [
1,
4,
],
}
CodePudding user response:
laravel collection intersect
intersect method removes any values from the original collection that are not present in the given array or collection.
$collection = collect([1,2,3,4]);
$collection2 = collect([1,5,6,4]);
$intersect = $collection->intersect($collection2);
$intersect->all();