Core\Arr::groupCounts
Core\Arr::groupCounts(array<T> $a, {by?: callable}): array<uint>Core\Arr::groupCounts( array<T> $a, {by?: callable}, ): array<uint>
Counts how many entries of an array fall under each distinct value.
You get back one entry per group. Its key is the value that was counted and its value is how many times that value appeared. The groups are in the order their first entry appeared, not in alphabetical order and not by size.
An array key can only be a whole number or text, so a counted value has to be one of those two. The number 1 and the text "1" name the same group, because that is how every array key works. A value that is neither a whole number nor text stops the program with an error.
The by option changes what is counted. It is a function that receives an entry and returns the name of the group that entry belongs to. With it you can count by one field of a record, or by any rule you can write.
The examples show how often each value appears, how to count by a field, and how to build a report of orders per day.
Parameters
$aarray<T>The array to count over.
Options
bycallableoptionalCalled once per entry as($value, $key), and may declare fewer parameters. It returns the group's name, anint, auintor astring. Without it, the value itself is the group.
Return value
Returns array<uint>. One uint per distinct group, keyed by the group, in the order each group first appears. 1 and "1" are the same group.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Arr::difference and Core\Arr::intersection compare two arrays. Core\Arr::groupCounts counts the elements of each group. The old names were diff, intersect and countBy. |
0.0.6 | A Core\Arr function that takes a key, such as hasKey, gave a compile error for a uint key (an unsigned whole number). It now takes a uint key, as $a[$key] already did. So Core\Arr::fillKeys($ids, true) works when $ids is a list of uint values. |
Examples
How often each value appears
<?nvs// Shows how to count how often each value appears in a list.
array<string> $answers = ['yes', 'no', 'yes', 'maybe', 'yes'];
// The result has one entry per distinct answer. The groups are in the order// they first appeared, so "yes" comes first with 3.array<uint> $howMany = Core\Arr::groupCounts($answers);echo Core\Json::encode($howMany), "\n";{"yes":3,"no":1,"maybe":1}Count by one field of a record
<?nvs// Shows how to count records by one of their fields. The `by` option is a// function that names the group each entry belongs to.
array<array<string>> $users = [ ['name' => 'Ada', 'city' => 'Berlin'], ['name' => 'Grace', 'city' => 'Hamburg'], ['name' => 'Alan', 'city' => 'Berlin'],];
// The function returns the city, so the cities become the keys of the result.// This prints `{"Berlin":2,"Hamburg":1}`.array<uint> $perCity = Core\Arr::groupCounts($users, { by: fn(array<string> $user): string => $user['city'],});echo Core\Json::encode($perCity), "\n";{"Berlin":2,"Hamburg":1}How many tickets per project
<?nvs// Shows how many tickets each project has, where the project is the first part// of the ticket number.
array<string> $tickets = [ 'BK-1042' => 'open', 'BK-0087' => 'closed', 'DV-5501' => 'open',];
// The function receives the value and the key of each entry. It uses the key// here, and returns the first two characters of it.array<uint> $perProject = Core\Arr::groupCounts($tickets, { by: fn(string $state, string $number): string => Core\Str::slice($number, 0, 2),});echo Core\Json::encode($perProject), "\n";{"BK":2,"DV":1}