3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [ '0' => [ 'id' => 34, 'building_id' => 786, 'tenancy_rate' => 0, 'rent_per_room' => 10000, 'management_fee_per_room' => 0, ], '1' => [ 'id' => 35, 'building_id' => 786, 'tenancy_rate' => 10, 'rent_per_room' => 11810, 'management_fee_per_room' => 5400, 'rent' => 86050, ], '2' => [ 'id' => 36, 'building_id' => 786, 'tenancy_rate' => 20, 'rent_per_room' => 11810, 'management_fee_per_room' => 5400, 'rent' => 86050, ], '3' => [ 'id' => 56, 'building_id' => 798, 'tenancy_rate' => 0, 'rent_per_room' => 10000, 'management_fee_per_room' => 5400, 'rent' => 77000, ], '4' => [ 'id' => 57, 'building_id' => 798, 'tenancy_rate' => 10, 'rent_per_room' => 11810, 'management_fee_per_room' => 5400, 'rent' => 86050, ], '5' => [ 'id' => 58, 'building_id' => 798, 'tenancy_rate' => 20, 'rent_per_room' => 11810, 'management_fee_per_room' => 5400, 'rent' => 86050, ], ]; $result = []; foreach ($arr as $val) { // as I see, rate for some array not there, so setting it to 0 $val['rent'] = ($val['rent'] ?? 0); if (isset($result[$val['tenancy_rate']]['rent'])) { $result[$val['tenancy_rate']]['rent'] += $val['rent']; } else { $result[$val['tenancy_rate']] = [ 'tenancy_rate' => $val['tenancy_rate'], 'rent' => $val['rent']]; } } print_r(array_values($result));
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 27
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 27
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 19
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
filename:       /in/Mm8P5
function name:  (null)
number of ops:  35
compiled vars:  !0 = $arr, !1 = $result, !2 = $val
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
   58     1        ASSIGN                                                   !1, <array>
   59     2      > FE_RESET_R                                       $5      !0, ->27
          3    > > FE_FETCH_R                                               $5, !2, ->27
   61     4    >   FETCH_DIM_IS                                     ~7      !2, 'rent'
          5        COALESCE                                         ~8      ~7
          6        QM_ASSIGN                                        ~8      0
          7        ASSIGN_DIM                                               !2, 'rent'
          8        OP_DATA                                                  ~8
   62     9        FETCH_DIM_R                                      ~9      !2, 'tenancy_rate'
         10        FETCH_DIM_IS                                     ~10     !1, ~9
         11        ISSET_ISEMPTY_DIM_OBJ                         0          ~10, 'rent'
         12      > JMPZ                                                     ~11, ->19
   63    13    >   FETCH_DIM_R                                      ~12     !2, 'tenancy_rate'
         14        FETCH_DIM_R                                      ~15     !2, 'rent'
         15        FETCH_DIM_RW                                     $13     !1, ~12
         16        ASSIGN_DIM_OP                +=               1          $13, 'rent'
         17        OP_DATA                                                  ~15
   62    18      > JMP                                                      ->26
   65    19    >   FETCH_DIM_R                                      ~16     !2, 'tenancy_rate'
   66    20        FETCH_DIM_R                                      ~18     !2, 'tenancy_rate'
         21        INIT_ARRAY                                       ~19     ~18, 'tenancy_rate'
         22        FETCH_DIM_R                                      ~20     !2, 'rent'
         23        ADD_ARRAY_ELEMENT                                ~19     ~20, 'rent'
   65    24        ASSIGN_DIM                                               !1, ~16
   66    25        OP_DATA                                                  ~19
   59    26    > > JMP                                                      ->3
         27    >   FE_FREE                                                  $5
   69    28        INIT_FCALL                                               'print_r'
         29        INIT_FCALL                                               'array_values'
         30        SEND_VAR                                                 !1
         31        DO_ICALL                                         $21     
         32        SEND_VAR                                                 $21
         33        DO_ICALL                                                 
         34      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.48 ms | 1004 KiB | 15 Q