3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = '[ { "key": "2.1", "value": "11", "quantity": "1" }, { "key": "2.1", "value": "22", "quantity": "1" }, { "key": "2.5", "value": "19", "quantity": "1" } ]'; $array = json_decode($json,true); $final = []; foreach($array as $arr){ $final[$arr['key']]['key'] = $arr['key']; $final[$arr['key']]['value'] = (isset($final[$arr['key']]['value'])) ? $final[$arr['key']]['value'].','. $arr['value'] : $arr['value']; $final[$arr['key']]['quantity'] = (isset($final[$arr['key']]['quantity'])) ? $final[$arr['key']]['quantity']+ $arr['quantity'] : $arr['quantity']; } echo $json = json_encode(array_values($final));
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 50
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 50
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 27
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 44
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 44
Branch analysis from position: 37
Branch analysis from position: 44
Branch analysis from position: 50
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 50
filename:       /in/H6grg
function name:  (null)
number of ops:  60
compiled vars:  !0 = $json, !1 = $array, !2 = $final, !3 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '%5B+%0A++++%7B+%22key%22%3A+%222.1%22%2C+%22value%22%3A+%2211%22%2C+%22quantity%22%3A+%221%22+%7D%2C+%0A++++%7B+%22key%22%3A+%222.1%22%2C+%22value%22%3A+%2222%22%2C+%22quantity%22%3A+%221%22+%7D%2C+%0A++++%7B+%22key%22%3A+%222.5%22%2C+%22value%22%3A+%2219%22%2C+%22quantity%22%3A+%221%22+%7D+%0A%5D'
    9     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $5      
          5        ASSIGN                                                   !1, $5
   11     6        ASSIGN                                                   !2, <array>
   13     7      > FE_RESET_R                                       $8      !1, ->50
          8    > > FE_FETCH_R                                               $8, !3, ->50
   15     9    >   FETCH_DIM_R                                      ~9      !3, 'key'
         10        FETCH_DIM_R                                      ~12     !3, 'key'
         11        FETCH_DIM_W                                      $10     !2, ~9
         12        ASSIGN_DIM                                               $10, 'key'
         13        OP_DATA                                                  ~12
   16    14        FETCH_DIM_R                                      ~13     !3, 'key'
         15        FETCH_DIM_R                                      ~16     !3, 'key'
         16        FETCH_DIM_IS                                     ~17     !2, ~16
         17        ISSET_ISEMPTY_DIM_OBJ                         0          ~17, 'value'
         18      > JMPZ                                                     ~18, ->27
         19    >   FETCH_DIM_R                                      ~19     !3, 'key'
         20        FETCH_DIM_R                                      ~20     !2, ~19
         21        FETCH_DIM_R                                      ~21     ~20, 'value'
         22        CONCAT                                           ~22     ~21, '%2C'
         23        FETCH_DIM_R                                      ~23     !3, 'value'
         24        CONCAT                                           ~24     ~22, ~23
         25        QM_ASSIGN                                        ~25     ~24
         26      > JMP                                                      ->29
         27    >   FETCH_DIM_R                                      ~26     !3, 'value'
         28        QM_ASSIGN                                        ~25     ~26
         29    >   FETCH_DIM_W                                      $14     !2, ~13
         30        ASSIGN_DIM                                               $14, 'value'
         31        OP_DATA                                                  ~25
   17    32        FETCH_DIM_R                                      ~27     !3, 'key'
         33        FETCH_DIM_R                                      ~30     !3, 'key'
         34        FETCH_DIM_IS                                     ~31     !2, ~30
         35        ISSET_ISEMPTY_DIM_OBJ                         0          ~31, 'quantity'
         36      > JMPZ                                                     ~32, ->44
         37    >   FETCH_DIM_R                                      ~33     !3, 'key'
         38        FETCH_DIM_R                                      ~34     !2, ~33
         39        FETCH_DIM_R                                      ~35     ~34, 'quantity'
         40        FETCH_DIM_R                                      ~36     !3, 'quantity'
         41        ADD                                              ~37     ~35, ~36
         42        QM_ASSIGN                                        ~38     ~37
         43      > JMP                                                      ->46
         44    >   FETCH_DIM_R                                      ~39     !3, 'quantity'
         45        QM_ASSIGN                                        ~38     ~39
         46    >   FETCH_DIM_W                                      $28     !2, ~27
         47        ASSIGN_DIM                                               $28, 'quantity'
         48        OP_DATA                                                  ~38
   13    49      > JMP                                                      ->8
         50    >   FE_FREE                                                  $8
   20    51        INIT_FCALL                                               'json_encode'
         52        INIT_FCALL                                               'array_values'
         53        SEND_VAR                                                 !2
         54        DO_ICALL                                         $40     
         55        SEND_VAR                                                 $40
         56        DO_ICALL                                         $41     
         57        ASSIGN                                           ~42     !0, $41
         58        ECHO                                                     ~42
         59      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
146.88 ms | 1017 KiB | 16 Q