3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = [ ['code'=>'aaa','name'=>'a','qty'=>20,'price'=>12.00], ['code'=>'bbb','name'=>'b','qty'=>20,'price'=>12.00], ['code'=>'ccc','name'=>'c','qty'=>20,'price'=>12.00], ['code'=>'aaa','name'=>'a','qty'=>20,'price'=>12.00], ['code'=>'ddd','name'=>'d','qty'=>20,'price'=>12.00], ['code'=>'ccc','name'=>'c','qty'=>5,'price'=>8.00], ['code'=>'bbb','name'=>'b','qty'=>15,'price'=>10.00], ['code'=>'ggg','name'=>'g','qty'=>20,'price'=>12.00] ]; $newdata = array_reduce(array_slice($data, 1), function ($c, $v) { if (($k = array_search($v['code'], array_column($c, 'code'))) !== false) { $c[$k]['qty'] += $v['qty']; $c[$k]['price'] += $v['price']; } else { $c[] = $v; } return $c; }, array($data[0])); print_r($newdata);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/hHZgS
function name:  (null)
number of ops:  18
compiled vars:  !0 = $data, !1 = $newdata
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
   10     1        INIT_FCALL                                               'array_reduce'
          2        INIT_FCALL                                               'array_slice'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 1
          5        DO_ICALL                                         $3      
          6        SEND_VAR                                                 $3
   11     7        DECLARE_LAMBDA_FUNCTION                          ~4      [0]
   20     8        SEND_VAL                                                 ~4
          9        FETCH_DIM_R                                      ~5      !0, 0
         10        INIT_ARRAY                                       ~6      ~5
         11        SEND_VAL                                                 ~6
   10    12        DO_ICALL                                         $7      
         13        ASSIGN                                                   !1, $7
   21    14        INIT_FCALL                                               'print_r'
         15        SEND_VAR                                                 !1
         16        DO_ICALL                                                 
         17      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 23
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/hHZgS
function name:  {closure}
number of ops:  27
compiled vars:  !0 = $c, !1 = $v, !2 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   12     2        INIT_FCALL                                               'array_search'
          3        FETCH_DIM_R                                      ~3      !1, 'code'
          4        SEND_VAL                                                 ~3
          5        INIT_FCALL                                               'array_column'
          6        SEND_VAR                                                 !0
          7        SEND_VAL                                                 'code'
          8        DO_ICALL                                         $4      
          9        SEND_VAR                                                 $4
         10        DO_ICALL                                         $5      
         11        ASSIGN                                           ~6      !2, $5
         12        TYPE_CHECK                                  1018          ~6
         13      > JMPZ                                                     ~7, ->23
   13    14    >   FETCH_DIM_R                                      ~10     !1, 'qty'
         15        FETCH_DIM_RW                                     $8      !0, !2
         16        ASSIGN_DIM_OP                +=               1          $8, 'qty'
         17        OP_DATA                                                  ~10
   14    18        FETCH_DIM_R                                      ~13     !1, 'price'
         19        FETCH_DIM_RW                                     $11     !0, !2
         20        ASSIGN_DIM_OP                +=               1          $11, 'price'
         21        OP_DATA                                                  ~13
   12    22      > JMP                                                      ->25
   17    23    >   ASSIGN_DIM                                               !0
         24        OP_DATA                                                  !1
   19    25    > > RETURN                                                   !0
   20    26*     > RETURN                                                   null

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.54 ms | 1020 KiB | 18 Q