3v4l.org

run code in 300+ PHP versions simultaneously
<?php $a = Array ( '0' => Array ( 'id' => 34, 'building_id' => 786, 'tenancy_rate' => 0, 'rent_per_room' => 10000, 'management_fee_per_room' => 0 ), '1' => Array ( 'id' => 35, 'building_id' => 786, 'tenancy_rate' => 10, 'rent_per_room' => 11810, 'management_fee_per_room' => 5400, 'rent' => 86050 ), '2' => Array ( 'id' => 36, 'building_id' => 786, 'tenancy_rate' => 20, 'rent_per_room' => 11810, 'management_fee_per_room' => 5400, 'rent' => 86050 ), '3' => Array ( 'id' => 56, 'building_id' => 798, 'tenancy_rate' => 0, 'rent_per_room' => 10000, 'management_fee_per_room' => 5400, 'rent' => 77000 ), '4' => Array ( 'id' => 57, 'building_id' => 798, 'tenancy_rate' => 10, 'rent_per_room' => 11810, 'management_fee_per_room' => 5400, 'rent' => 86050 ), '5' => Array ( 'id' => 58, 'building_id' => 798, 'tenancy_rate' => 20, 'rent_per_room' => 11810, 'management_fee_per_room' => 5400, 'rent' => 86050 ) ); $f = []; foreach($a as $v){ if(!empty($f[$v['tenancy_rate']])){ $f[$v['tenancy_rate']]['rent'] += $v['rent']; }else{ $f[$v['tenancy_rate']] = [ 'tenancy_rate' => $v['tenancy_rate'], 'rent' => isset($v['rent']) ? $v['rent'] : 0 ]; } } print_r($f);

preferences:
32.62 ms | 404 KiB | 5 Q