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));

preferences:
26.34 ms | 406 KiB | 5 Q