3v4l.org

run code in 500+ PHP versions simultaneously
<?php $sampleData = [ ['group' => 'g1', 'branch' => 'b1', 'unit' => 'null'], ['group' => 'g1', 'branch' => 'b1', 'unit' => 'u2'], ['group' => 'g2', 'branch' => 'null', 'unit' => 'u3'], ['group' => 'g2', 'branch' => 'b6', 'unit' => 'u4'], ['group' => 'g3', 'branch' => 'b7', 'unit' => 'u5'], ]; $groups = []; foreach($sampleData as $row){ $groups[$row['group']][$row['branch']][] = $row['unit']; } var_dump($groups);
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
array(3) { ["g1"]=> array(1) { ["b1"]=> array(2) { [0]=> string(4) "null" [1]=> string(2) "u2" } } ["g2"]=> array(2) { ["null"]=> array(1) { [0]=> string(2) "u3" } ["b6"]=> array(1) { [0]=> string(2) "u4" } } ["g3"]=> array(1) { ["b7"]=> array(1) { [0]=> string(2) "u5" } } }

preferences:
94.7 ms | 1166 KiB | 4 Q