feat: add Liquid filter 'group_by'
Some checks are pending
tests / ci (push) Waiting to run

This commit is contained in:
Benjamin Nussbaum 2025-08-25 14:43:22 +02:00
parent f4f8ab5181
commit 25f36eaf54
3 changed files with 152 additions and 0 deletions

View file

@ -39,4 +39,28 @@ class Data extends FiltersProvider
return $fallback;
}
/**
* Group a collection by a specific key
*
* @param array $collection The collection to group
* @param string $key The key to group by
* @return array The grouped collection
*/
public function group_by(array $collection, string $key): array
{
$grouped = [];
foreach ($collection as $item) {
if (is_array($item) && array_key_exists($key, $item)) {
$groupKey = $item[$key];
if (! isset($grouped[$groupKey])) {
$grouped[$groupKey] = [];
}
$grouped[$groupKey][] = $item;
}
}
return $grouped;
}
}