3v4l.org

run code in 300+ PHP versions simultaneously
<?php $a = Array ( '0' => Array ( 'location_id' => 1, 'property_id' => 10 ), '1' => Array ( 'location_id' => 2, 'property_id' => 20 ), '2' => Array ( 'location_id' => 3, 'property_id' => 10 ), '3' => Array ( 'location_id' => 4, 'property_id' => 10 ), '4' => Array ( 'location_id' => 5, 'property_id' => 10 ), '5' => Array ( 'location_id' => 6, 'property_id' => 20 ) ); $r = []; foreach($a as $v){ if(isset($r[$v['property_id']])){ array_push($r[$v['property_id']]['location_id'], $v['location_id']); }else{ $r[$v['property_id']] = [ 'property_id' => $v['property_id'], 'location_id' => [$v['location_id']] ]; } } print_r(array_values($r));
Output for 7.1.25 - 7.1.32, 7.2.0 - 7.2.33, 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.27, 8.4.1 - 8.4.14
Array ( [0] => Array ( [property_id] => 10 [location_id] => Array ( [0] => 1 [1] => 3 [2] => 4 [3] => 5 ) ) [1] => Array ( [property_id] => 20 [location_id] => Array ( [0] => 2 [1] => 6 ) ) )

preferences:
153.93 ms | 409 KiB | 5 Q