YaLinqo\Enumerable::except PHP Method

except() public method

Element keys are values identifying elements. They are used as array keys and are subject to the same rules as array keys, for example, integer 100 and string "100" are considered equal.

Syntax: distinct (other)

Produces the set difference of two sequences using values as element keys.

Syntax: distinct (other, keySelector {(v, k) ==> value})

Produces the set difference of two sequences using values produced by keySelector as element keys.

public except ( array | Iterator | IteratorAggregate | Enumerable $other, callable | null $keySelector = null ) : Enumerable
$other array | Iterator | IteratorAggregate | Enumerable A sequence whose elements that also occur in the source sequence will cause those elements to be removed from the returned sequence.
$keySelector callable | null {(v, k) ==> key} A function to extract the element key from each element. Default: value.
return Enumerable A sequence that contains the set difference of the elements of two sequences.
    public function except($other, $keySelector = null)
    {
        $other = self::from($other);
        $keySelector = Utils::createLambda($keySelector, 'v,k', Functions::$value);
        return new self(function () use($other, $keySelector) {
            $set = [];
            foreach ($other as $k => $v) {
                $key = $keySelector($v, $k);
                $set[$key] = true;
            }
            foreach ($this as $k => $v) {
                $key = $keySelector($v, $k);
                if (isset($set[$key])) {
                    continue;
                }
                $set[$key] = true;
                (yield $k => $v);
            }
        });
    }