3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Fetch data from BD // $options = $stmt->query("SELECT * FROM Options")->fetch_all(PDO::FETCH_ASSOC); $options = [ ['id' => 1, 'name' => 'admin', 'ref' => 0], ['id' => 2, 'name' => 'A', 'ref' => 1], ['id' => 3, 'name' => 'B', 'ref' => 2], ['id' => 4, 'name' => 'C', 'ref' => 1], ['id' => 5, 'name' => 'D', 'ref' => 2], ['id' => 6, 'name' => 'E', 'ref' => 3], ['id' => 7, 'name' => 'F', 'ref' => 3], ['id' => 8, 'name' => 'G', 'ref' => 2], ]; $nodes = array(); $roots = array(); // init nodes indexed by IDs foreach ($options as $option) { $option['subIds'] = array(); // init subIds $nodes[$option['id']] = $option; } // build a recursive structure (by reference) foreach ($options as $option) { if ($option['ref'] == 0) { $roots[] = $option['id']; // add a root } else { $nodes[$option['ref']]['subIds'][] = $option['id']; // add a subnode } } // build recursive HTML-List function getSubtreeHTMLList($subOptionIds, $nodes) { $result = '<ul>'; foreach ($subOptionIds as $optionsId) { $result .= '<li>'; $result .= $nodes[$optionsId]['name']; if (count($nodes[$optionsId]['subIds'] > 0)) { $result .= getSubtreeHTMLList($nodes[$optionsId]['subIds'], $nodes); } $result .= '</li>'; } $result .= '</ul>'; return $result; } echo getSubtreeHTMLList($roots, $nodes); ?>
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 11
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 11
2 jumps found. (Code = 77) Position 1 = 13, Position 2 = 28
Branch analysis from position: 13
2 jumps found. (Code = 78) Position 1 = 14, Position 2 = 28
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 21
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
Branch analysis from position: 11
filename:       /in/Q7X8F
function name:  (null)
number of ops:  35
compiled vars:  !0 = $options, !1 = $nodes, !2 = $roots, !3 = $option
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   ASSIGN                                                   !0, <array>
   19     1        ASSIGN                                                   !1, <array>
   20     2        ASSIGN                                                   !2, <array>
   23     3      > FE_RESET_R                                       $7      !0, ->11
          4    > > FE_FETCH_R                                               $7, !3, ->11
   24     5    >   ASSIGN_DIM                                               !3, 'subIds'
          6        OP_DATA                                                  <array>
   25     7        FETCH_DIM_R                                      ~9      !3, 'id'
          8        ASSIGN_DIM                                               !1, ~9
          9        OP_DATA                                                  !3
   23    10      > JMP                                                      ->4
         11    >   FE_FREE                                                  $7
   29    12      > FE_RESET_R                                       $11     !0, ->28
         13    > > FE_FETCH_R                                               $11, !3, ->28
   30    14    >   FETCH_DIM_R                                      ~12     !3, 'ref'
         15        IS_EQUAL                                                 ~12, 0
         16      > JMPZ                                                     ~13, ->21
   31    17    >   FETCH_DIM_R                                      ~15     !3, 'id'
         18        ASSIGN_DIM                                               !2
         19        OP_DATA                                                  ~15
   30    20      > JMP                                                      ->27
   33    21    >   FETCH_DIM_R                                      ~16     !3, 'ref'
         22        FETCH_DIM_R                                      ~20     !3, 'id'
         23        FETCH_DIM_W                                      $17     !1, ~16
         24        FETCH_DIM_W                                      $18     $17, 'subIds'
         25        ASSIGN_DIM                                               $18
         26        OP_DATA                                                  ~20
   29    27    > > JMP                                                      ->13
         28    >   FE_FREE                                                  $11
   52    29        INIT_FCALL                                               'getsubtreehtmllist'
         30        SEND_VAR                                                 !2
         31        SEND_VAR                                                 !1
         32        DO_FCALL                                      0  $21     
         33        ECHO                                                     $21
   54    34      > RETURN                                                   1

Function getsubtreehtmllist:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 24
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 24
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 22
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 22
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/Q7X8F
function name:  getSubtreeHTMLList
number of ops:  28
compiled vars:  !0 = $subOptionIds, !1 = $nodes, !2 = $result, !3 = $optionsId
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   39     2        ASSIGN                                                   !2, '%3Cul%3E'
   40     3      > FE_RESET_R                                       $5      !0, ->24
          4    > > FE_FETCH_R                                               $5, !3, ->24
   41     5    >   ASSIGN_OP                                     8          !2, '%3Cli%3E'
   42     6        FETCH_DIM_R                                      ~7      !1, !3
          7        FETCH_DIM_R                                      ~8      ~7, 'name'
          8        ASSIGN_OP                                     8          !2, ~8
   43     9        FETCH_DIM_R                                      ~10     !1, !3
         10        FETCH_DIM_R                                      ~11     ~10, 'subIds'
         11        IS_SMALLER                                       ~12     0, ~11
         12        COUNT                                            ~13     ~12
         13      > JMPZ                                                     ~13, ->22
   44    14    >   INIT_FCALL_BY_NAME                                       'getSubtreeHTMLList'
         15        CHECK_FUNC_ARG                                           
         16        FETCH_DIM_FUNC_ARG                               $14     !1, !3
         17        FETCH_DIM_FUNC_ARG                               $15     $14, 'subIds'
         18        SEND_FUNC_ARG                                            $15
         19        SEND_VAR_EX                                              !1
         20        DO_FCALL                                      0  $16     
         21        ASSIGN_OP                                     8          !2, $16
   46    22    >   ASSIGN_OP                                     8          !2, '%3C%2Fli%3E'
   40    23      > JMP                                                      ->4
         24    >   FE_FREE                                                  $5
   48    25        ASSIGN_OP                                     8          !2, '%3C%2Ful%3E'
   49    26      > RETURN                                                   !2
   50    27*     > RETURN                                                   null

End of function getsubtreehtmllist

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
252.97 ms | 1008 KiB | 14 Q