Core\Arr::any
Core\Arr::any(array<T> $a, callable(T, string): bool $predicate): boolCore\Arr::any( array<T> $a, callable(T, string): bool $predicate, ): bool
Tests whether at least one entry in an array meets a condition that you write.
You pass the array and a small function. Novis calls that function with one entry at a time, and the function returns true or false. The first time it returns true, Core\Arr::any stops there and the result is true. The entries after that one are never read. If no entry returns true, the result is false.
Your function may take a second parameter. Novis then passes the key of the entry as well, always as a string. In a plain list the key is the position, so it is "0", then "1", then "2".
Good to know: an empty array gives false. There is no entry that can match. Use Core\Arr::find when you need the matching entry itself instead of a yes or no.
Parameters
$aarray<T>The array to test.$predicatecallable(T, string): boolCalled as($value, $key)per entry, and may declare fewer parameters.
Return value
Returns bool. true at the first truthy result; false for the empty array.
Examples
One article is out of stock
<?nvs// Shows how to test whether at least one number in a list is zero.
array<int> $stock = [5, 3, 0, 8];// The third entry is 0, so this prints "one article is out of stock".if (Core\Arr::any($stock, fn(int $left) => $left == 0)) { echo 'one article is out of stock', "\n";} else { echo 'every article is in stock', "\n";}
array<int> $refilled = [5, 3, 2, 8];// No entry is 0. The result is false, so this prints the other line.if (Core\Arr::any($refilled, fn(int $left) => $left == 0)) { echo 'one article is out of stock', "\n";} else { echo 'every article is in stock', "\n";}one article is out of stockevery article is in stockA header name starts with x
<?nvs// Shows how to test the key of each entry. The second parameter of your// function is the key, always as a string.
array<string> $headers = ['accept' => 'text/html', 'x-request-id' => 'a41f'];// The second key starts with "x-", so this prints "a custom header is set".if (Core\Arr::any($headers, fn(string $value, string $name) => Core\Str::startsWith($name, 'x-'))) { echo 'a custom header is set', "\n";} else { echo 'no custom header is set', "\n";}
array<string> $empty = [];// An empty array gives false. There is no entry that can match.if (Core\Arr::any($empty, fn(string $value, string $name) => Core\Str::startsWith($name, 'x-'))) { echo 'a custom header is set', "\n";} else { echo 'an empty list has no match', "\n";}a custom header is setan empty list has no matchThe user may open the page
<?nvs// Shows a check a program makes before it opens a page. The user needs one of// the roles the page allows.
array<string> $allowed = ['editor', 'admin'];array<string> $roles = ['author', 'editor'];
// "editor" is in both lists, so this prints "the page opens".if (Core\Arr::any($roles, fn(string $role) => Core\Arr::contains($allowed, $role))) { echo 'the page opens', "\n";} else { echo 'the page stays closed', "\n";}the page opens