Skip to content

Core\Arr::groupBy

Sorts the values of an array into groups. A function names the group each value belongs to, and the result has one array per group.

You give Core\Arr::groupBy an array and a function. The function receives the value and the key, and returns the name of the group. A function that needs only the value declares only that one parameter. A group name is a whole number or a text.

A group appears in the result when its first value does, so the order follows the array you gave. Inside a group, every value keeps the key it had. That tells you which entries ended up together. Use Core\Arr::values on a group when you want a plain list instead.

Good to know: to count the entries per group instead of collecting them, use Core\Arr::groupCounts.

Parameters

  • $aarray<T>The array to put into groups.
  • $keycallable(T, string): int|uint|stringCalled as ($value, $key) per entry, and may declare fewer parameters. It returns the group's name, an int, a uint or a string.

Return value

Returns array<array<T>>. One inner array per distinct result, in the order each result first appears. Each inner array keeps its entries under their own keys. 1 and "1" name the same group.

Changelog

VersionChange
0.0.5Core\Arr::groupBy and Core\Arr::groupCounts copy a group's key only when the group is new. They allocate less memory and run faster on large arrays.
0.0.6A 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

Sort values into groups

<?nvs
// Sorts the values into groups. Here every word goes into the group named by
// its first letter.
array<string> $words = ['red', 'rust', 'blue', 'green'];
array<array<string>> $byLetter = Core\Arr::groupBy($words, fn(string $word)
=> Core\Str::slice($word, 0, 1));
// This prints `r: 2`, then `b: 1` and `g: 1`. A group appears where its first
// word appeared in the list.
foreach ($byLetter as string $letter => array<string> $group) {
echo $letter, ': ', Core\Arr::count($group), "\n";
}
Output
r: 2
b: 1
g: 1

Add up the numbers of each day

<?nvs
// Groups counts by the day they were measured, and adds up each day. The
// function reads the key here, because the day is part of the key.
array<int> $visits = [
'2026-05-01 09:00' => 12,
'2026-05-01 17:30' => 30,
'2026-05-02 08:15' => 7,
];
// The first 10 characters of the key are the date.
array<array<int>> $perDay = Core\Arr::groupBy($visits, fn(int $count, string $at)
=> Core\Str::slice($at, 0, 10));
// This prints `2026-05-01: 42` and `2026-05-02: 7`.
foreach ($perDay as string $day => array<int> $counts) {
echo $day, ': ', Core\Arr::sum($counts), "\n";
}
Output
2026-05-01: 42
2026-05-02: 7

Show the orders of each state

<?nvs
// Sorts orders into groups by their state, and lists each group. A shop shows
// this on a page that says how much work is waiting.
array<string> $orders = [
'o-101' => 'paid',
'o-102' => 'open',
'o-103' => 'paid',
'o-104' => 'sent',
];
array<array<string>> $byState = Core\Arr::groupBy($orders, fn(string $state) => $state);
// Every order keeps its own key inside the group, so the order numbers are still there.
// This prints `paid: 2 (o-101, o-103)`, then one line for `open` and one for `sent`.
foreach ($byState as string $state => array<string> $group) {
echo $state, ': ', Core\Arr::count($group),
' (', Core\Str::join(Core\Arr::keys($group), ', '), ')', "\n";
}
Output
paid: 2 (o-101, o-103)
open: 1 (o-102)
sent: 1 (o-104)