Source Code:
(back to article)
<?php $data = [['id' => 1, 'name' => 'Alice', 'group' => 'A'], ['id' => 2, 'name' => 'Bob', 'group' => 'B'], ['id' => 3, 'name' => 'Charlie', 'group' => 'A'], ['id' => 4, 'name' => 'Dave', 'group' => 'B']]; $grouped = array_reduce( $data, function ($carry, $item) { $carry[$item['group']][] = $item; return $carry; }, [] ); print_r($grouped);
Result:
Report an issue