Laravel 5 Collections: Filtering Collections Based On Value Presence With intersect

April 22, 2018 —John Koster

The intersect removes any values that are not in the provided $items array. The intersect method returns a new instance of Collection. The intersect method preserves any keys from the original collection.

The behavior of the intersect method is similar to PHP's array_intersect function.


1public function intersect(
2 $items

#Example Use

The following code example highlights the usage of the intersect method:

1use Illuminate\Support\Collection;
3// Create a new collection instance.
4$collection = new Collection([
5 'first', 'second', 'third'
8// An empty collection will be returned.
9$intersected = $collection->intersect(['fourth']);
11// A collection only containing the 'third' value will
12// be returned.
13$intersected = $collection->intersect(['fourth', 'third']);

Some absolutely amazing

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.