3v4l.org

run code in 300+ PHP versions simultaneously
<?php $data = [ 'Orders' => [ [ 'transaction_id' => '78078', 'date' => 1616277653, 'currency' => 'EUR', 'items' => [ ['title' => 'Title 1', 'quantity' => 200, 'price' => 2.1, 'sku' => '1T.810BK'], ['title' => 'Title 2', 'quantity' => 250, 'price' => 1.78, 'sku' => '1090.005BK'], ], ], [ 'transaction_id' => 78079, 'date' => 1616597838, 'currency' => 'EUR', 'items' => [ ['title' => 'title 3', 'quantity' => 3, 'price' => 118.3, 'sku' => '1.DIYBOX.ULT.638RE'], ], ], [ 'transaction_id' => 78080, 'date' => 1616598322, 'currency' => 'EUR', 'items' => [ ['title' => 'title 2', 'quantity' => 300, 'price' => 1.78, 'sku' => '1090.005BK'], ], ] ] ]; $result = []; foreach ($data['Orders'] as $order) { foreach ($order['items'] as $item) { $result[$item['sku']] = ($result[$item['sku']] ?? 0) + $item['quantity']; } } var_export($result); echo "\n===\n"; var_export( array_column(array_merge_recursive(... array_column($data['Orders'], 'items')), 'quantity', 'sku') ); echo "\n===\n"; $sku = ''; $qt = -1; $result = []; array_walk_recursive($data, function($v, $k) use(&$sku,&$qt,&$result) { if($k === 'quantity'){ if($sku !== ''){ $result[ $sku ] = $v; $sku = ''; }else { $qt = $v; } }else if($k === 'sku'){ if($qt !== -1){ $result[ $v ] = $qt; $qt = -1; }else{ $sku = $v; } } }); var_export($result);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 20
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 20
Branch analysis from position: 5
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 18
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 18
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 18
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/ZCRTu
function name:  (null)
number of ops:  58
compiled vars:  !0 = $data, !1 = $result, !2 = $order, !3 = $item, !4 = $sku, !5 = $qt
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   33     1        ASSIGN                                                   !1, <array>
   34     2        FETCH_DIM_R                                      ~8      !0, 'Orders'
          3      > FE_RESET_R                                       $9      ~8, ->20
          4    > > FE_FETCH_R                                               $9, !2, ->20
   35     5    >   FETCH_DIM_R                                      ~10     !2, 'items'
          6      > FE_RESET_R                                       $11     ~10, ->18
          7    > > FE_FETCH_R                                               $11, !3, ->18
   36     8    >   FETCH_DIM_R                                      ~12     !3, 'sku'
          9        FETCH_DIM_R                                      ~14     !3, 'sku'
         10        FETCH_DIM_IS                                     ~15     !1, ~14
         11        COALESCE                                         ~16     ~15
         12        QM_ASSIGN                                        ~16     0
         13        FETCH_DIM_R                                      ~17     !3, 'quantity'
         14        ADD                                              ~18     ~16, ~17
         15        ASSIGN_DIM                                               !1, ~12
         16        OP_DATA                                                  ~18
   35    17      > JMP                                                      ->7
         18    >   FE_FREE                                                  $11
   34    19      > JMP                                                      ->4
         20    >   FE_FREE                                                  $9
   39    21        INIT_FCALL                                               'var_export'
         22        SEND_VAR                                                 !1
         23        DO_ICALL                                                 
   41    24        ECHO                                                     '%0A%3D%3D%3D%0A'
   42    25        INIT_FCALL                                               'var_export'
   43    26        INIT_FCALL                                               'array_column'
         27        INIT_FCALL                                               'array_merge_recursive'
         28        INIT_FCALL                                               'array_column'
         29        FETCH_DIM_R                                      ~20     !0, 'Orders'
         30        SEND_VAL                                                 ~20
         31        SEND_VAL                                                 'items'
         32        DO_ICALL                                         $21     
         33        SEND_UNPACK                                              $21
         34        CHECK_UNDEF_ARGS                                         
         35        DO_ICALL                                         $22     
         36        SEND_VAR                                                 $22
         37        SEND_VAL                                                 'quantity'
         38        SEND_VAL                                                 'sku'
         39        DO_ICALL                                         $23     
         40        SEND_VAR                                                 $23
   42    41        DO_ICALL                                                 
   46    42        ECHO                                                     '%0A%3D%3D%3D%0A'
   47    43        ASSIGN                                                   !4, ''
   48    44        ASSIGN                                                   !5, -1
   49    45        ASSIGN                                                   !1, <array>
   50    46        INIT_FCALL                                               'array_walk_recursive'
         47        SEND_REF                                                 !0
         48        DECLARE_LAMBDA_FUNCTION                          ~28     [0]
         49        BIND_LEXICAL                                             ~28, !4
         50        BIND_LEXICAL                                             ~28, !5
         51        BIND_LEXICAL                                             ~28, !1
   66    52        SEND_VAL                                                 ~28
   50    53        DO_ICALL                                                 
   67    54        INIT_FCALL                                               'var_export'
         55        SEND_VAR                                                 !1
         56        DO_ICALL                                                 
         57      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 15
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 24
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 23
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/ZCRTu
function name:  {closure}
number of ops:  25
compiled vars:  !0 = $v, !1 = $k, !2 = $sku, !3 = $qt, !4 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
          4        BIND_STATIC                                              !4
   51     5        IS_IDENTICAL                                             !1, 'quantity'
          6      > JMPZ                                                     ~5, ->15
   52     7    >   IS_NOT_IDENTICAL                                         !2, ''
          8      > JMPZ                                                     ~6, ->13
   53     9    >   ASSIGN_DIM                                               !4, !2
         10        OP_DATA                                                  !0
   54    11        ASSIGN                                                   !2, ''
   52    12      > JMP                                                      ->14
   56    13    >   ASSIGN                                                   !3, !0
   51    14    > > JMP                                                      ->24
   58    15    >   IS_IDENTICAL                                             !1, 'sku'
         16      > JMPZ                                                     ~10, ->24
   59    17    >   IS_NOT_IDENTICAL                                         !3, -1
         18      > JMPZ                                                     ~11, ->23
   60    19    >   ASSIGN_DIM                                               !4, !0
         20        OP_DATA                                                  !3
   61    21        ASSIGN                                                   !3, -1
   59    22      > JMP                                                      ->24
   63    23    >   ASSIGN                                                   !2, !0
   66    24    > > RETURN                                                   null

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.6 ms | 1014 KiB | 17 Q