3v4l.org

run code in 500+ PHP versions simultaneously
<?php $data = [ [ 'id' => 2, 'order_id' => 1, 'adm_aftr_disc' => 25.5, ], [ 'id' => 4, 'order_id' => 2, 'adm_aftr_disc' => 10, ], [ 'id' => 6, 'order_id' => 1, 'adm_aftr_disc' => 50, ], ]; print_r(groupData($data)); function groupData(array $data) { // Our return array $ret = []; foreach($data as $item) { // This is what we're grouping on $orderId = $item['order_id']; // If it doesn't exist in the return array, add it with sane defaults if(!array_key_exists($orderId, $ret)){ $ret[$orderId] = [ 'order_id' => $orderId, 'adm_aftr_disc' => 0, ]; } // Sum. Add more as needed $ret[$orderId]['adm_aftr_disc'] += $item['adm_aftr_disc']; } return $ret; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0gnEj
function name:  (null)
number of ops:  8
compiled vars:  !0 = $data
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
   21     1        INIT_FCALL                                                   'print_r'
          2        INIT_FCALL_BY_NAME                                           'groupData'
          3        SEND_VAR_EX                                                  !0
          4        DO_FCALL                                          0  $2      
          5        SEND_VAR                                                     $2
          6        DO_ICALL                                                     
   46     7      > RETURN                                                       1

Function groupdata:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 18
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 18
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 13
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/0gnEj
function name:  groupData
number of ops:  21
compiled vars:  !0 = $data, !1 = $ret, !2 = $item, !3 = $orderId
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   23     0  E >   RECV                                                 !0      
   26     1        ASSIGN                                                       !1, <array>
   28     2      > FE_RESET_R                                           $5      !0, ->18
          3    > > FE_FETCH_R                                                   $5, !2, ->18
   31     4    >   FETCH_DIM_R                                          ~6      !2, 'order_id'
          5        ASSIGN                                                       !3, ~6
   34     6        ARRAY_KEY_EXISTS                                     ~8      !3, !1
          7        BOOL_NOT                                             ~9      ~8
          8      > JMPZ                                                         ~9, ->13
   36     9    >   INIT_ARRAY                                           ~11     !3, 'order_id'
   37    10        ADD_ARRAY_ELEMENT                                    ~11     0, 'adm_aftr_disc'
   35    11        ASSIGN_DIM                                                   !1, !3
   37    12        OP_DATA                                                      ~11
   42    13    >   FETCH_DIM_R                                          ~14     !2, 'adm_aftr_disc'
         14        FETCH_DIM_RW                                         $12     !1, !3
         15        ASSIGN_DIM_OP                    +=               1          $12, 'adm_aftr_disc'
         16        OP_DATA                                                      ~14
   28    17      > JMP                                                          ->3
         18    >   FE_FREE                                                      $5
   45    19      > RETURN                                                       !1
   46    20*     > RETURN                                                       null

End of function groupdata

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
162.26 ms | 1915 KiB | 14 Q