Skip to content

IEnumerable.any() method

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

IEnumerable->any([$predicate]) method

Determines whether any element of the sequence exists or satisfies a condition. (s. Any).

Syntax

public function any([ predicate $predicate = null ]) : bool;

Parameters

Name Type Description
$predicate [[predicate Predicate]]

$predicate

The condition to use.

Result

TRUE if condition is satisfied / at least one element exists; otherwise FALSE.

Examples

Lambda expressions

use \System\Linq\Enumerable;

$seq1 = Enumerable::fromValues(5979, 'TM', null, "MK", 23979);
$seq2 = Enumerable::fromValues();
$seq3 = Enumerable::fromValues(1, 2, 3);

// (true), because 3rd element is (null)
$a1 = $seq1->any('$x => null === $x');

// (false), because sequence contains no elements
$a2 = $seq2->any();

// (true), because sequence contains that least one element
$a3 = $seq3->any();

Closures

use \System\Linq\Enumerable;

$seq1 = Enumerable::fromValues(null, 5979, null);
$seq2 = Enumerable::fromValues();
$seq3 = Enumerable::fromValues(1, 2, 3);

// (true), because 2nd element is NOT (null)
$a1 = $seq1->any(function ($x) {
                    return $x !== null;
                 });

// (false), because sequence contains no elements
$a2 = $seq2->any();

// (true), because sequence contains that least one element
$a3 = $seq3->any();
Clone this wiki locally