Skip to content

IEnumerable.except() method

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

IEnumerable->except($second [, $comparer]) method

Produces the difference between that sequence and another (s. Except()).

Syntax

public function except(sequence $second
                       [, equality_comparer $equalityComparer = null ]) : IEnumerable;
Name Type Description
$second [[sequence Sequence]]
$equalityComparer [[equality_comparer Equality comparer]]

$second

The other sequence.

$equalityComparer

The equality comparer that checks if two items of the sequences are the same.

Result

The new sequence.

Examples

Default

use \System\Linq\Enumerable;

$seq = Enumerable::fromValues(1, 2, 3, 4, 5);
$exc = Enumerable::fromValues(4, 1);

foreach ($seq->except($exc) as $item) {
    // [0] 2
    // [1] 3
    // [2] 5
}

Custom comparer (lambda expression)

use \System\Linq\Enumerable;

$seq = Enumerable::fromValues(1, 2, 3, 4.0, 5);
$exc = Enumerable::fromValues(4, 1);

foreach ($seq->except($exc, '($x, $y) => $x === $y') as $item) {
    // [0] 2
    // [1] 3
    // [2] 4.0
    // [3] 5
}

Custom comparer (closure)

use \System\Linq\Enumerable;

$seq = Enumerable::fromValues(1, 2, 3, 4.0, 5);
$exc = Enumerable::fromValues(4, 1);

foreach ($seq->except($exc, function($x, $y) {
                                return $x === $y; 
                            }) as $item) {
    // [0] 2
    // [1] 3
    // [2] 4.0
    // [3] 5
}
Clone this wiki locally