mirror of
https://github.com/usetrmnl/byos_laravel.git
synced 2026-01-13 23:18:10 +00:00
This commit is contained in:
parent
f4f8ab5181
commit
25f36eaf54
3 changed files with 152 additions and 0 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue