Skip to content

IEnumerable.contains() method

Marcel Kloubert edited this page Sep 30, 2015 · 5 revisions

IEnumerable->contains($item [, $equalityComparer]) method

Determines whether the sequence contains a specified element (s. Contains(TSource)).

Syntax

public function contains(mixed $item
                         [, equality_comparer $equalityComparer = null ]) : bool;

Parameters

Name Type Description
$item mixed The element to search for.
$equalityComparer [[equality_comparer Equality comparer]]

$item

The element to search for.

$equalityComparer

Compare two elements if they are the same.

Result

TRUE if element was found; otherwise FALSE.

Examples

Default

use \System\Linq\Enumerable;

$seq = Enumerable::fromValues(5979, 'TM', null);

// (true)
$a1 = $seq->contains("TM");

// (false)
$a2 = $seq->reset()
          ->contains(23979);

Custom comparer (lambda expression)

use \System\Linq\Enumerable;

$seq = Enumerable::fromValues('tm', 'MK');

// (true)
$a = $seq->contains("TM", '($x, $y) => 0 === strcasecmp($x, $y)');

Custom comparer (closure)

use \System\Linq\Enumerable;

$seq = Enumerable::fromValues('tm', 'MK');

// (true)
$a = $seq->contains("MK", function($x, $y) {
                              return 0 === strcasecmp($x, $y); 
                          });
Clone this wiki locally