Core\Arr::all
Core\Arr::all(array<T> $a, callable(T, string): bool $predicate): boolCore\Arr::all( array<T> $a, callable(T, string): bool $predicate, ): bool
Tests whether every 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. If it returns false for an entry, Core\Arr::all stops there and the result is false. The entries after that one are never read. If every entry returns true, the result is true.
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 true. There is no entry that can fail, so nothing makes the result false.
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. false at the first falsy result; true for the empty array.
Examples
Every number is above zero
<?nvs// Shows how to test that every number in a list is above zero.
array<int> $quantities = [2, 1, 4];// Every entry is above zero, so this prints "every quantity is above zero".if (Core\Arr::all($quantities, fn(int $n) => $n > 0)) { echo 'every quantity is above zero', "\n";} else { echo 'one quantity is zero or less', "\n";}
array<int> $withZero = [2, 0, 4];// The second entry is 0. The result is false, so this prints the other line.if (Core\Arr::all($withZero, fn(int $n) => $n > 0)) { echo 'every quantity is above zero', "\n";} else { echo 'one quantity is zero or less', "\n";}every quantity is above zeroone quantity is zero or lessEvery address has an at sign
<?nvs// Shows the same test over text. `Core\Str::contains` tests one address, and// `Core\Arr::all` runs that test over the whole list.
array<string> $addresses = ['ada@example.com', 'bob@example.com'];// Both addresses contain "@", so this prints "every address has an @".if (Core\Arr::all($addresses, fn(string $address) => Core\Str::contains($address, '@'))) { echo 'every address has an @', "\n";} else { echo 'one address has no @', "\n";}
array<string> $empty = [];// An empty array gives true. There is no entry that can fail the test.if (Core\Arr::all($empty, fn(string $address) => Core\Str::contains($address, '@'))) { echo 'an empty list passes too', "\n";}every address has an @an empty list passes tooEnough of every article in stock
<?nvs// Shows a check a shop makes before it accepts an order. Every article in the// basket needs enough left in stock.
// The keys are article numbers. The values are how many the customer wants.array<int> $basket = ['NVS-100' => 2, 'NVS-220' => 1];array<int> $stock = ['NVS-100' => 5, 'NVS-220' => 0];
// The second parameter of your function is the key of the entry, as a string.// Nothing is left of "NVS-220", so this prints "an article is out of stock".if (Core\Arr::all($basket, fn(int $wanted, string $article) => $wanted <= $stock[$article])) { echo 'order accepted', "\n";} else { echo 'an article is out of stock', "\n";}an article is out of stock