3v4l.org

run code in 300+ PHP versions simultaneously
<?php $results = [ [ 'id' => '1000', 'name' => 'Nurten', 'pId' => '26', 'anzahl' => '1', 'pName' => 'Fitness-Teller', 'preis' => '12.90', ], [ 'id' => '1001', 'name' => 'Kutscha', 'pId' => '94', 'anzahl' => '1', 'pName' => 'Pizza Parma', 'preis' => '12.90', ], [ 'id' => '1001', 'name' => 'Kutscha', 'pId' => '75', 'anzahl' => '1', 'pName' => 'Pizza Margherita', 'preis' => '6.50', ], ]; $out = []; foreach ($results as $result) { if (!isset($out[$result['id']])) { $out[$result['id']] = [ 'id' => $result['id'], 'name' => $result['name'], ]; } $out[$result['id']]['products'][] = [ 'pId' => $result['pId'], 'anzahl' => $result['anzahl'], 'pName' => $result['pName'], 'preis' => $result['preis'], ]; } print_r(array_values($out));
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.13
Array ( [0] => Array ( [id] => 1000 [name] => Nurten [products] => Array ( [0] => Array ( [pId] => 26 [anzahl] => 1 [pName] => Fitness-Teller [preis] => 12.90 ) ) ) [1] => Array ( [id] => 1001 [name] => Kutscha [products] => Array ( [0] => Array ( [pId] => 94 [anzahl] => 1 [pName] => Pizza Parma [preis] => 12.90 ) [1] => Array ( [pId] => 75 [anzahl] => 1 [pName] => Pizza Margherita [preis] => 6.50 ) ) ) )

preferences:
120.82 ms | 410 KiB | 5 Q