3v4l.org

run code in 300+ 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:  31
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
   30     9    >   INIT_FCALL                                               'sprintf'
   31    10        SEND_VAL                                                 '%3Cli%3E%25s+%25s%3C%2Fli%3E'
   32    11        FETCH_DIM_R                                      ~9      !2, 'name'
         12        SEND_VAL                                                 ~9
   33    13        INIT_FCALL_BY_NAME                                       'nested2ul'
         14        CHECK_FUNC_ARG                                           
         15        FETCH_DIM_FUNC_ARG                               $10     !2, 'children'
         16        SEND_FUNC_ARG                                            $10
         17        DO_FCALL                                      0  $11     
         18        SEND_VAR                                                 $11
         19        DO_ICALL                                         $12     
   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    >   INIT_FCALL                                               'implode'
         27        SEND_VAR                                                 !1
         28        DO_ICALL                                         $14     
         29      > RETURN                                                   $14
   39    30*     > RETURN                                                   null

End of function nested2ul

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
166.21 ms | 1411 KiB | 21 Q