3v4l.org

run code in 500+ PHP versions simultaneously
<?php $two_dimention = [ ["id" => 4, "name" => "Home", "parent" => 0, "depth" => 0], ["id" => 5, "name" => "Menu 1", "parent" => 0, "depth" => 0], ["id" => 6, "name" => "Menu 2", "parent" => 0, "depth" => 0], ["id" => 8, "name" => "Menu 2.1", "parent" => 6, "depth" => 1], ["id" => 10, "name" => "Menu 2.1.1", "parent" => 8, "depth" => 2], ["id" => 11, "name" => "Menu 2.1.2", "parent" => 8, "depth" => 2], ["id" => 9, "name" => "Menu 2.2", "parent" => 6, "depth" => 1], ["id" => 7, "name" => "Menu 3", "parent" => 0, "depth" => 0], ["id" => 18, "name" => "Menu 3.1", "parent" => 7, "depth" => 1], ]; function makeRecursive($d, $r = 0, $pk = 'parent', $k = 'id', $c = 'children') { $m = []; foreach ($d as $e) { isset($m[$e[$pk]]) ?: $m[$e[$pk]] = []; isset($m[$e[$k]]) ?: $m[$e[$k]] = []; $m[$e[$pk]][] = array_merge($e, [$c => &$m[$e[$k]]]); } return $m[$r]; // remove [0] if there could be more than one root nodes } function nested2ul($data) { $result = []; if (sizeof($data) > 0) { $result[] = '<ul>'; foreach ($data as $entry) { $result[] = sprintf( '<li>%s %s</li>', $entry['name'], nested2ul($entry['children']) ); } $result[] = '</ul>'; } return implode($result); } $temp= makeRecursive($two_dimention); echo nested2ul($temp);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EeU1P
function name:  (null)
number of ops:  10
compiled vars:  !0 = $two_dimention, !1 = $temp
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
   40     1        INIT_FCALL                                                   'makerecursive'
          2        SEND_VAR                                                     !0
          3        DO_FCALL                                          0  $3      
          4        ASSIGN                                                       !1, $3
   41     5        INIT_FCALL                                                   'nested2ul'
          6        SEND_VAR                                                     !1
          7        DO_FCALL                                          0  $5      
          8        ECHO                                                         $5
          9      > RETURN                                                       1

Function makerecursive:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 36
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 36
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 36
filename:       /in/EeU1P
function name:  makeRecursive
number of ops:  40
compiled vars:  !0 = $d, !1 = $r, !2 = $pk, !3 = $k, !4 = $c, !5 = $m, !6 = $e
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   14     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      0
          2        RECV_INIT                                            !2      'parent'
          3        RECV_INIT                                            !3      'id'
          4        RECV_INIT                                            !4      'children'
   16     5        ASSIGN                                                       !5, <array>
   17     6      > FE_RESET_R                                           $8      !0, ->36
          7    > > FE_FETCH_R                                                   $8, !6, ->36
   18     8    >   FETCH_DIM_R                                          ~9      !6, !2
          9        ISSET_ISEMPTY_DIM_OBJ                             0  ~10     !5, ~9
         10        JMP_SET                                              ~11     ~10, ->15
         11        FETCH_DIM_R                                          ~12     !6, !2
         12        ASSIGN_DIM                                           ~13     !5, ~12
         13        OP_DATA                                                      <array>
         14        QM_ASSIGN                                            ~11     ~13
         15        FREE                                                         ~11
   19    16        FETCH_DIM_R                                          ~14     !6, !3
         17        ISSET_ISEMPTY_DIM_OBJ                             0  ~15     !5, ~14
         18        JMP_SET                                              ~16     ~15, ->23
         19        FETCH_DIM_R                                          ~17     !6, !3
         20        ASSIGN_DIM                                           ~18     !5, ~17
         21        OP_DATA                                                      <array>
         22        QM_ASSIGN                                            ~16     ~18
         23        FREE                                                         ~16
   20    24        FETCH_DIM_R                                          ~19     !6, !2
         25        INIT_FCALL                                                   'array_merge'
         26        SEND_VAR                                                     !6
         27        FETCH_DIM_R                                          ~22     !6, !3
         28        FETCH_DIM_W                                          $23     !5, ~22
         29        INIT_ARRAY                                           ~24     $23, !4
         30        SEND_VAL                                                     ~24
         31        DO_ICALL                                             $25     
         32        FETCH_DIM_W                                          $20     !5, ~19
         33        ASSIGN_DIM                                                   $20
         34        OP_DATA                                                      $25
   17    35      > JMP                                                          ->7
         36    >   FE_FREE                                                      $8
   22    37        FETCH_DIM_R                                          ~26     !5, !1
         38      > RETURN                                                       ~26
   23    39*     > RETURN                                                       null

End of function makerecursive

Function nested2ul:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 26
Branch analysis from position: 5
2 jumps found. (Code = 77) Position 1 = 8, Position 2 = 23
Branch analysis from position: 8
2 jumps found. (Code = 78) Position 1 = 9, Position 2 = 23
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
Branch analysis from position: 26
filename:       /in/EeU1P
function name:  nested2ul
number of ops:  29
compiled vars:  !0 = $data, !1 = $result, !2 = $entry
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   24     0  E >   RECV                                                 !0      
   26     1        ASSIGN                                                       !1, <array>
   27     2        COUNT                                                ~4      !0
          3        IS_SMALLER                                                   0, ~4
          4      > JMPZ                                                         ~5, ->26
   28     5    >   ASSIGN_DIM                                                   !1
          6        OP_DATA                                                      '%3Cul%3E'
   29     7      > FE_RESET_R                                           $7      !0, ->23
          8    > > FE_FETCH_R                                                   $7, !2, ->23
   32     9    >   FETCH_DIM_R                                          ~9      !2, 'name'
   33    10        INIT_FCALL_BY_NAME                                           'nested2ul'
         11        CHECK_FUNC_ARG                                               
         12        FETCH_DIM_FUNC_ARG                                   $10     !2, 'children'
         13        SEND_FUNC_ARG                                                $10
         14        DO_FCALL                                          0  $11     
         15        ROPE_INIT                                         5  ~13     '%3Cli%3E'
         16        ROPE_ADD                                          1  ~13     ~13, ~9
         17        ROPE_ADD                                          2  ~13     ~13, '+'
         18        ROPE_ADD                                          3  ~13     ~13, $11
         19        ROPE_END                                          4  ~12     ~13, '%3C%2Fli%3E'
   30    20        ASSIGN_DIM                                                   !1
   33    21        OP_DATA                                                      ~12
   29    22      > JMP                                                          ->8
         23    >   FE_FREE                                                      $7
   36    24        ASSIGN_DIM                                                   !1
         25        OP_DATA                                                      '%3C%2Ful%3E'
   38    26    >   FRAMELESS_ICALL_1                implode             ~17     !1
         27      > RETURN                                                       ~17
   39    28*     > RETURN                                                       null

End of function nested2ul

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
175.2 ms | 2146 KiB | 16 Q