3v4l.org

run code in 500+ PHP versions simultaneously
<?php $array = [ ['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] ]; $result = []; foreach ($array as $row) { if (!isset($result[$row['color']])) { $result[$row['color']] = $row; } elseif (isset($result[$row['color']]['count'])) { $result[$row['color']]['size'] = [ [ 'value' => $result[$row['color']]['size'], 'count' => $result[$row['color']]['count'] ], [ 'value' => $row['size'], 'count' => $row['count'] ] ]; unset($result[$row['color']]['count']); } else { $result[$row['color']]['size'][] = [ 'value' => $row['size'], 'count' => $row['count'] ]; } } echo json_encode(array_values($result), JSON_PRETTY_PRINT);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 48
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 48
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 38
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
filename:       /in/ZFEnP
function name:  (null)
number of ops:  58
compiled vars:  !0 = $array, !1 = $result, !2 = $row
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
   11     1        ASSIGN                                                       !1, <array>
   12     2      > FE_RESET_R                                           $5      !0, ->48
          3    > > FE_FETCH_R                                                   $5, !2, ->48
   13     4    >   FETCH_DIM_R                                          ~6      !2, 'color'
          5        ISSET_ISEMPTY_DIM_OBJ                             0  ~7      !1, ~6
          6        BOOL_NOT                                             ~8      ~7
          7      > JMPZ                                                         ~8, ->12
   14     8    >   FETCH_DIM_R                                          ~9      !2, 'color'
          9        ASSIGN_DIM                                                   !1, ~9
         10        OP_DATA                                                      !2
   13    11      > JMP                                                          ->47
   15    12    >   FETCH_DIM_R                                          ~11     !2, 'color'
         13        FETCH_DIM_IS                                         ~12     !1, ~11
         14        ISSET_ISEMPTY_DIM_OBJ                             0          ~12, 'count'
         15      > JMPZ                                                         ~13, ->38
   16    16    >   FETCH_DIM_R                                          ~14     !2, 'color'
   18    17        FETCH_DIM_R                                          ~17     !2, 'color'
         18        FETCH_DIM_R                                          ~18     !1, ~17
         19        FETCH_DIM_R                                          ~19     ~18, 'size'
         20        INIT_ARRAY                                           ~20     ~19, 'value'
   19    21        FETCH_DIM_R                                          ~21     !2, 'color'
         22        FETCH_DIM_R                                          ~22     !1, ~21
         23        FETCH_DIM_R                                          ~23     ~22, 'count'
         24        ADD_ARRAY_ELEMENT                                    ~20     ~23, 'count'
         25        INIT_ARRAY                                           ~24     ~20
   22    26        FETCH_DIM_R                                          ~25     !2, 'size'
         27        INIT_ARRAY                                           ~26     ~25, 'value'
   23    28        FETCH_DIM_R                                          ~27     !2, 'count'
         29        ADD_ARRAY_ELEMENT                                    ~26     ~27, 'count'
         30        ADD_ARRAY_ELEMENT                                    ~24     ~26
   16    31        FETCH_DIM_W                                          $15     !1, ~14
         32        ASSIGN_DIM                                                   $15, 'size'
   23    33        OP_DATA                                                      ~24
   26    34        FETCH_DIM_R                                          ~28     !2, 'color'
         35        FETCH_DIM_UNSET                                      $29     !1, ~28
         36        UNSET_DIM                                                    $29, 'count'
   15    37      > JMP                                                          ->47
   28    38    >   FETCH_DIM_R                                          ~30     !2, 'color'
   29    39        FETCH_DIM_R                                          ~34     !2, 'size'
         40        INIT_ARRAY                                           ~35     ~34, 'value'
   30    41        FETCH_DIM_R                                          ~36     !2, 'count'
         42        ADD_ARRAY_ELEMENT                                    ~35     ~36, 'count'
   28    43        FETCH_DIM_W                                          $31     !1, ~30
         44        FETCH_DIM_W                                          $32     $31, 'size'
         45        ASSIGN_DIM                                                   $32
   30    46        OP_DATA                                                      ~35
   12    47    > > JMP                                                          ->3
         48    >   FE_FREE                                                      $5
   34    49        INIT_FCALL                                                   'json_encode'
         50        INIT_FCALL                                                   'array_values'
         51        SEND_VAR                                                     !1
         52        DO_ICALL                                             $37     
         53        SEND_VAR                                                     $37
         54        SEND_VAL                                                     128
         55        DO_ICALL                                             $38     
         56        ECHO                                                         $38
         57      > RETURN                                                       1

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
168.45 ms | 1990 KiB | 15 Q