Core\Arr::count
Counts the entries in an array.
The result is a whole number, and 0 for an array with nothing in it. Core\Arr::count counts the entries at the top level only. An entry that is itself an array counts as one, so counting the values inside that entry is a second call.
The keys make no difference to the result. An array whose two keys are 0 and 1000000 has two entries, so its count is 2.
Good to know: Core\Arr::isEmpty tests whether an array has anything in it, and needs no comparison with 0. To count the characters of a text, use Core\Str::length.
Parameters
$aarray<T>The array to count.
Return value
Returns uint. The number of entries; 0 for the empty array.
Examples
How many entries are in a list
<?nvs// Shows how to count the entries of an array. `Core\Arr::count` returns a whole// number, and `0` for an array with nothing in it.
array<string> $guests = ['Mara', 'Ida', 'Tom'];// This prints 3.echo Core\Arr::count($guests), "\n";
array<string> $waiting = [];// This prints 0.echo Core\Arr::count($waiting), "\n";30Count rows that hold arrays
<?nvs// Shows what `Core\Arr::count` counts when an array holds other arrays. It// counts the entries at the top level, so every team below counts as one entry.
array<array<string>> $teams = [ 'red' => ['Mara', 'Ida'], 'blue' => ['Tom', 'Nia', 'Sam'],];
// This prints 2, because there are two teams.echo Core\Arr::count($teams), "\n";
// Counting the people in a team is a second call, one team at a time.// This prints `red: 2` and `blue: 3`.foreach ($teams as string $name => array<string> $members) { echo $name, ': ', Core\Arr::count($members), "\n";}2red: 2blue: 3Check a batch against a limit
<?nvs// Counts the rows a request sent, before any work starts on them. A program does// this so that one request cannot ask the server for too much at once.
array<string> $rows = ['a1', 'b2', 'c3', 'd4', 'e5'];uint $limit = 4;uint $sent = Core\Arr::count($rows);
// This prints `5 rows is more than the limit of 4`, and nothing is imported.if ($sent > $limit) { echo $sent, ' rows is more than the limit of ', $limit, "\n";} else { echo 'importing ', $sent, ' rows', "\n";}5 rows is more than the limit of 4