3v4l.org

run code in 500+ PHP versions simultaneously
<?php $input = [['color'=>'#000000','size'=>'L','count'=>2], ['color'=>'#000000','size'=>'XL','count'=>1], ['color'=>'#ffffff','size'=>'L','count'=>2], ['color'=>'#ffffff','size'=>'XL','count'=>1], ['color'=>'#ff0000','size'=>'L','count'=>1]]; $colors = []; foreach ($input as $product) { extract($product); $colors[$color][$size] = $count; } $output = []; foreach ($colors as $color => $sizes) { $data = []; foreach ($sizes as $size => $count) { $data[] = ['value' => $size, 'count' => $count]; } if (count($data) == 1) { $output[] = ['color' => $color, 'size' => $data[0]['value'], 'count' => $data[0]['count']]; } else { $output[] = ['color' => $color, 'size' => $data]; } } echo json_encode($output, JSON_PRETTY_PRINT);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 11
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 14, Position 2 = 44
Branch analysis from position: 14
2 jumps found. (Code = 78) Position 1 = 15, Position 2 = 44
Branch analysis from position: 15
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 25
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 25
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 39
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
Branch analysis from position: 25
Branch analysis from position: 44
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 44
Branch analysis from position: 11
filename:       /in/6f1ku
function name:  (null)
number of ops:  51
compiled vars:  !0 = $input, !1 = $colors, !2 = $product, !3 = $color, !4 = $size, !5 = $count, !6 = $output, !7 = $sizes, !8 = $data
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
    9     1        ASSIGN                                                       !1, <array>
   11     2      > FE_RESET_R                                           $11     !0, ->11
          3    > > FE_FETCH_R                                                   $11, !2, ->11
   12     4    >   INIT_FCALL                                                   'extract'
          5        SEND_REF                                                     !2
          6        DO_ICALL                                                     
   13     7        FETCH_DIM_W                                          $13     !1, !3
          8        ASSIGN_DIM                                                   $13, !4
          9        OP_DATA                                                      !5
   11    10      > JMP                                                          ->3
         11    >   FE_FREE                                                      $11
   16    12        ASSIGN                                                       !6, <array>
   18    13      > FE_RESET_R                                           $16     !1, ->44
         14    > > FE_FETCH_R                                           ~17     $16, !7, ->44
         15    >   ASSIGN                                                       !3, ~17
   19    16        ASSIGN                                                       !8, <array>
   20    17      > FE_RESET_R                                           $20     !7, ->25
         18    > > FE_FETCH_R                                           ~21     $20, !5, ->25
         19    >   ASSIGN                                                       !4, ~21
   21    20        INIT_ARRAY                                           ~24     !4, 'value'
   22    21        ADD_ARRAY_ELEMENT                                    ~24     !5, 'count'
   21    22        ASSIGN_DIM                                                   !8
   22    23        OP_DATA                                                      ~24
   20    24      > JMP                                                          ->18
         25    >   FE_FREE                                                      $20
   24    26        COUNT                                                ~25     !8
         27        IS_EQUAL                                                     ~25, 1
         28      > JMPZ                                                         ~26, ->39
   25    29    >   INIT_ARRAY                                           ~28     !3, 'color'
   26    30        FETCH_DIM_R                                          ~29     !8, 0
         31        FETCH_DIM_R                                          ~30     ~29, 'value'
         32        ADD_ARRAY_ELEMENT                                    ~28     ~30, 'size'
   27    33        FETCH_DIM_R                                          ~31     !8, 0
         34        FETCH_DIM_R                                          ~32     ~31, 'count'
         35        ADD_ARRAY_ELEMENT                                    ~28     ~32, 'count'
   25    36        ASSIGN_DIM                                                   !6
   27    37        OP_DATA                                                      ~28
   24    38      > JMP                                                          ->43
   29    39    >   INIT_ARRAY                                           ~34     !3, 'color'
   30    40        ADD_ARRAY_ELEMENT                                    ~34     !8, 'size'
   29    41        ASSIGN_DIM                                                   !6
   30    42        OP_DATA                                                      ~34
   18    43    > > JMP                                                          ->14
         44    >   FE_FREE                                                      $16
   34    45        INIT_FCALL                                                   'json_encode'
         46        SEND_VAR                                                     !6
         47        SEND_VAL                                                     128
         48        DO_ICALL                                             $35     
         49        ECHO                                                         $35
         50      > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
144.2 ms | 1990 KiB | 15 Q