3v4l.org

run code in 500+ PHP versions simultaneously
<?php $json = '[ {"id":3633,"name":"Mobile phones and accessories","left":1,"right":18,"level":1,"elements":0}, {"id":3638,"name":"Mobile phones","left":2,"right":3,"level":2,"elements":174}, {"id":21396,"name":"Tablets","left":19,"right":24,"level":1,"elements":0}, {"id":21450,"name":"Some Tablets","left":20,"right":21,"level":2,"elements":8}, {"id":21450,"name":"other Tablets","left":20,"right":21,"level":2,"elements":8} ]'; $data = new ArrayIterator(json_decode($json, true)); function buildTree(iterator $data, int $level = 1): array { $tree = array(); while ($data->valid()) { $item = $data->current(); if ($item['level'] < $level) return $tree; if ($item['level'] === $level) { $data->next(); $tree[] = [ 'item' => $item, 'children' => buildTree($data, $level + 1), ]; } } return $tree; } $tree = buildTree($data); print_r($tree); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AmhMX
function name:  (null)
number of ops:  17
compiled vars:  !0 = $json, !1 = $data, !2 = $tree
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                       !0, '%5B%0A++++%7B%22id%22%3A3633%2C%22name%22%3A%22Mobile+phones+and+accessories%22%2C%22left%22%3A1%2C%22right%22%3A18%2C%22level%22%3A1%2C%22elements%22%3A0%7D%2C%0A++++%7B%22id%22%3A3638%2C%22name%22%3A%22Mobile+phones%22%2C%22left%22%3A2%2C%22right%22%3A3%2C%22level%22%3A2%2C%22elements%22%3A174%7D%2C%0A++++%0A++++%7B%22id%22%3A21396%2C%22name%22%3A%22Tablets%22%2C%22left%22%3A19%2C%22right%22%3A24%2C%22level%22%3A1%2C%22elements%22%3A0%7D%2C%0A++++%7B%22id%22%3A21450%2C%22name%22%3A%22Some+Tablets%22%2C%22left%22%3A20%2C%22right%22%3A21%2C%22level%22%3A2%2C%22elements%22%3A8%7D%2C%0A++++%7B%22id%22%3A21450%2C%22name%22%3A%22other+Tablets%22%2C%22left%22%3A20%2C%22right%22%3A21%2C%22level%22%3A2%2C%22elements%22%3A8%7D%0A++++%5D'
   13     1        NEW                                                  $4      'ArrayIterator'
          2        INIT_FCALL                                                   'json_decode'
          3        SEND_VAR                                                     !0
          4        SEND_VAL                                                     <true>
          5        DO_ICALL                                             $5      
          6        SEND_VAR_NO_REF_EX                                           $5
          7        DO_FCALL                                          0          
          8        ASSIGN                                                       !1, $4
   44     9        INIT_FCALL                                                   'buildtree'
         10        SEND_VAR                                                     !1
         11        DO_FCALL                                          0  $8      
         12        ASSIGN                                                       !2, $8
   46    13        INIT_FCALL                                                   'print_r'
         14        SEND_VAR                                                     !2
         15        DO_ICALL                                                     
   48    16      > RETURN                                                       1

Function buildtree:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 4
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 26
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 4
Branch analysis from position: 29
Branch analysis from position: 4
Branch analysis from position: 26
filename:       /in/AmhMX
function name:  buildTree
number of ops:  33
compiled vars:  !0 = $data, !1 = $level, !2 = $tree, !3 = $item
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   15     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      1
   17     2        ASSIGN                                                       !2, <array>
   20     3      > JMP                                                          ->26
   21     4    >   INIT_METHOD_CALL                                             !0, 'current'
          5        DO_FCALL                                          0  $5      
          6        ASSIGN                                                       !3, $5
   23     7        FETCH_DIM_R                                          ~7      !3, 'level'
          8        IS_SMALLER                                                   ~7, !1
          9      > JMPZ                                                         ~8, ->12
   24    10    >   VERIFY_RETURN_TYPE                                           !2
         11      > RETURN                                                       !2
   27    12    >   FETCH_DIM_R                                          ~9      !3, 'level'
         13        IS_IDENTICAL                                                 !1, ~9
         14      > JMPZ                                                         ~10, ->26
   28    15    >   INIT_METHOD_CALL                                             !0, 'next'
         16        DO_FCALL                                          0          
   31    17        INIT_ARRAY                                           ~13     !3, 'item'
   32    18        INIT_FCALL_BY_NAME                                           'buildTree'
         19        SEND_VAR_EX                                                  !0
         20        ADD                                                  ~14     !1, 1
         21        SEND_VAL_EX                                                  ~14
         22        DO_FCALL                                          0  $15     
         23        ADD_ARRAY_ELEMENT                                    ~13     $15, 'children'
   30    24        ASSIGN_DIM                                                   !2
   32    25        OP_DATA                                                      ~13
   20    26    >   INIT_METHOD_CALL                                             !0, 'valid'
         27        DO_FCALL                                          0  $16     
         28      > JMPNZ                                                        $16, ->4
   41    29    >   VERIFY_RETURN_TYPE                                           !2
         30      > RETURN                                                       !2
   42    31*       VERIFY_RETURN_TYPE                                           
         32*     > RETURN                                                       null

End of function buildtree

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
150.61 ms | 1732 KiB | 16 Q