Skip to content

IEnumerable.where() method

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

IEnumerable->where($predicate) method

Filters the elements of that sequence (s. Where()).

Syntax

public function where(predicate $predicate) : IEnumerable;

Parameters

Name Type Description
$predicate [[predicate Predicate]]

$predicate

The condition to use.

Result

The new filtered sequence.

Examples

Lambda expression

use \System\Linq\Enumerable;

$seq = Enumerable::create([1, 2, 3, 4, 5, 6, 8]);

foreach ($seq->where('$i => ($i % 2) === 0') as $item) {
    // [0] 2
    // [1] 4
    // [2] 6
    // [3] 8
}

Closure

use \System\Linq\Enumerable;

$seq = Enumerable::create([1, 2, 3, 4, 5, 6, 8]);

foreach ($seq->where(function($i) {
                         return ($i % 2) === 0;
                     }) as $item) {
    
    // [0] 2
    // [1] 4
    // [2] 6
    // [3] 8
}
Clone this wiki locally