Core\Arr::isEmpty
Tests whether an array holds no entries at all.
The result is true when the array has no entries, and false when it has at least one. A value of null, 0 or an empty text is still an entry, so an array holding one of those is not empty. What counts is how many entries are stored, never what is stored in them.
Core\Arr::isEmpty reads a number the array already keeps, so it does not look at the entries. The answer costs the same for three entries and for three million.
Good to know: this is the way to tell an empty array from one whose first entry is null. Core\Arr::first returns null for both.
Parameters
$aarray<T>The array to test.
Return value
Returns bool. true for the empty array, false otherwise.
Examples
An empty basket
<?nvs// Shows how to test whether an array holds no entries.
array<string> $basket = [];array<string> $wishlist = ['a mug', 'a poster'];
// The basket holds no entries. This prints "the basket is empty".if (Core\Arr::isEmpty($basket)) { echo 'the basket is empty', "\n";}
// The wishlist holds two entries. This prints "the wishlist has entries".if (Core\Arr::isEmpty($wishlist)) { echo 'the wishlist is empty', "\n";} else { echo 'the wishlist has entries', "\n";}the basket is emptythe wishlist has entriesAn entry that holds nothing
<?nvs// Shows what counts as an entry. An empty text is still an entry, so the// array is not empty. Removing that entry leaves an array that is.
array<string> $lines = [''];array<string> $nothingLeft = Core\Arr::withoutLast($lines);
// This prints "one entry: no" and then "nothing left: yes".echo 'one entry: ', Core\Arr::isEmpty($lines) ? 'yes' : 'no', "\n";echo 'nothing left: ', Core\Arr::isEmpty($nothingLeft) ? 'yes' : 'no', "\n";one entry: nonothing left: yesNothing found for this search
<?nvs// Shows the check a page makes after a search. When no article matched, the// page says so instead of printing an empty list.
array<string> $catalogue = ['red mug', 'blue mug', 'green cap'];array<string> $matches = Core\Arr::filter($catalogue, fn(string $item) => Core\Str::contains($item, 'hat'));
// No article contains "hat". This prints "nothing found".if (Core\Arr::isEmpty($matches)) { echo 'nothing found', "\n";} else { foreach ($matches as string $match) { echo $match, "\n"; }}nothing found