November 29, 2016 —John Koster
contains($key, $value = null)
The contains
method is used to determine if a given $key
exists within the collection. Additionally, an option $value
can be specified that will be used to check if a given key/value pair exists within the collection. The contains
method is incredibly versatile, and each use case will be highlighted below.
The following demonstrates the contains
method in its most basic usage:
1<?php 2 3use Illuminate\Support\Collection; 4 5// Create a new collection instance. 6$collection = new Collection([ 7 'bear', 8 'whale', 9 'chicken',10 'tardigrade'11]);12 13// true14$collection->contains('bear');15 16// true17$collection->contains('tardigrade');18 19// false20$collection->contains('dog');
The following demonstrates using the contains
method to check if a given key/value pair exists in a collection that contains arrays as items:
1<?php 2 3use Illuminate\Support\Collection; 4 5// Create a new collection instance. 6$collection = new Collection([ 7 ['big' => 'bear'], 8 ['bigger' => 'whale'], 9 ['small' => 'chicken'],10 ['smaller' => 'tardigrade']11]);12 13// true14$collection->contains('big', 'bear');15 16// true17$collection->contains('smaller', 'tardigrade');18 19// false20$collection->contains('smaller', 'paramecium');
The following example shows how the contains
method can be used on collections of objects, using a hypothetical User
class:
1<?php 2 3use Illuminate\Support\Collection; 4 5class User 6{ 7 8 /** 9 * A hypothetical user's first name.10 *11 * @var string12 */13 public $firstName = '';14 15 /**16 * A hypothetical user's favorite number.17 *18 * @var integer19 */20 public $favoriteNumber = 0;21 22 public function __construct($firstName, $favoriteNumber)23 {24 $this->firstName = $firstName;25 $this->favoriteNumber = $favoriteNumber;26 }27 28}29 30// Create a new collection instance.31$collection = new Collection([32 new User('Jane', 7),33 new User('Sarah', 9),34 new User('Ben', 2)35]);36 37// true38$collection->contains('firstName', 'Jane');39 40// false41$collection->contains('firstName', 'Josh');42 43// false44$collection->contains('lastName', 'Jane');45 46// true47$collection->contains('favoriteNumber', 2);
A function can also be supplied as the only argument to the contains
method. The following example demonstrates how to determine if there are any user's that have favorite numbers greater than 7
:
1<?php2 3// true4$collection->contains(function($key, $value) {5 return $value->favoriteNumber > 7;6});
It should be noted that in the above example that the $value
will be an instance of the User
class. Additionally, truth tests can be as complex as required. The following will determine if there are any favorite numbers between 7
and 9
, including 7
and 9
:
1<?php2 3// true4$collection->contains(function($key, $value) {5 return ($value->favoriteNumber >= 7 && $value->favoriteNumber <= 9);6});
contains
methodThe contains
method defines two parameters: $key
and $value
. The $value
parameter has a default value of null
, however, the two method calls are not the same:
1<?php 2 3use Illuminate\Support\Collection; 4 5$collection = new Collection([ 6 'first', 'second', 'third' 7]); 8 9// true10$collection->contains('third');11 12// true13$collection->contains('third', null);
Even though the two examples return true
, the second example is functionally not correct. When explicitly passing null
as the second argument, the contains
method will always return true
:
1<?php2 3// true4$collection->contains('fourth', null);
The above example returns true
even though fourth
is not in the collection.
∎
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.