3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [ 5 => [ "data_id" => "5", "data_parent" => "4", "data_level" => " ", "data_prefix" => " ", "data_title" => "Assets", "data_link" => "0", ], 57 => [ "data_id" => "57", "data_parent" => "5", "data_level" => " ", "data_prefix" => " ", "data_title" => "Fixed Assets", "data_link" => "0", ], 52 => [ "data_id" => "52", "data_parent" => "5", "data_level" => " ", "data_prefix" => " ", "data_title" => "Asset Two", "data_link" => "1", ], 51 => [ "data_id" => "51", "data_parent" => "5", "data_level" => " ", "data_prefix" => " ", "data_title" => "Assset ONE", "data_link" => "1", ], 48 => [ "data_id" => "48", "data_parent" => "4", "data_level" => " ", "data_prefix" => " ", "data_title" => "Expenses", "data_link" => "0", ], 50 => [ "data_id" => "50", "data_parent" => "48", "data_level" => " ", "data_prefix" => " ", "data_title" => "Expense One", "data_link" => "1", ], 49 => [ "data_id" => "49", "data_parent" => "48", "data_level" => " ", "data_prefix" => " ", "data_title" => "Expense One", "data_link" => "1", ], 58 => [ "data_id" => "58", "data_parent" => "57", "data_level" => " ", "data_prefix" => " ", "data_title" => "Vehicles", "data_link" => "1", ], ]; function buildTree(array $elements, $options = [ 'parent_id_column_name' => 'parent_id', 'children_key_name' => 'children', 'id_column_name' => 'id', ], $parentId = 0) { $branch = []; foreach ($elements as $element) { if ($element[$options['parent_id_column_name']] == $parentId) { $children = buildTree($elements, $options, $element[$options['id_column_name']]); if ($children) { $element[$options['children_key_name']] = $children; } $branch[] = $element; } } return $branch; } // 4 as first parent is $temp = buildTree($arr, [ 'parent_id_column_name' => 'data_parent', 'children_key_name' => 'children', 'id_column_name' => 'data_id'], 4); function printTree($tree) { if (!is_null($tree) && count($tree) > 0) { echo '<ul>'; foreach ($tree as $node) { // you can use whole $node here including all values of each node echo '<li>' . $node['data_title']; if (!empty($node['children'])) { printTree($node['children']); } echo '</li>'; } echo '</ul>'; } } printTree($temp);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7rtJU
function name:  (null)
number of ops:  11
compiled vars:  !0 = $arr, !1 = $temp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   ASSIGN                                                   !0, <array>
  103     1        INIT_FCALL                                               'buildtree'
          2        SEND_VAR                                                 !0
  104     3        SEND_VAL                                                 <array>
  106     4        SEND_VAL                                                 4
  103     5        DO_FCALL                                      0  $3      
          6        ASSIGN                                                   !1, $3
  122     7        INIT_FCALL                                               'printtree'
          8        SEND_VAR                                                 !1
          9        DO_FCALL                                      0          
         10      > RETURN                                                   1

Function buildtree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 26
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 26
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 25
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 23
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 23
Branch analysis from position: 25
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
filename:       /in/7rtJU
function name:  buildTree
number of ops:  29
compiled vars:  !0 = $elements, !1 = $options, !2 = $parentId, !3 = $branch, !4 = $element, !5 = $children
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   85     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
          2        RECV_INIT                                        !2      0
   90     3        ASSIGN                                                   !3, <array>
   91     4      > FE_RESET_R                                       $7      !0, ->26
          5    > > FE_FETCH_R                                               $7, !4, ->26
   92     6    >   FETCH_DIM_R                                      ~8      !1, 'parent_id_column_name'
          7        FETCH_DIM_R                                      ~9      !4, ~8
          8        IS_EQUAL                                                 !2, ~9
          9      > JMPZ                                                     ~10, ->25
   93    10    >   INIT_FCALL_BY_NAME                                       'buildTree'
         11        SEND_VAR_EX                                              !0
         12        SEND_VAR_EX                                              !1
         13        CHECK_FUNC_ARG                                           
         14        FETCH_DIM_R                                      ~11     !1, 'id_column_name'
         15        FETCH_DIM_FUNC_ARG                               $12     !4, ~11
         16        SEND_FUNC_ARG                                            $12
         17        DO_FCALL                                      0  $13     
         18        ASSIGN                                                   !5, $13
   94    19      > JMPZ                                                     !5, ->23
   95    20    >   FETCH_DIM_R                                      ~15     !1, 'children_key_name'
         21        ASSIGN_DIM                                               !4, ~15
         22        OP_DATA                                                  !5
   97    23    >   ASSIGN_DIM                                               !3
         24        OP_DATA                                                  !4
   91    25    > > JMP                                                      ->5
         26    >   FE_FREE                                                  $7
  100    27      > RETURN                                                   !3
  101    28*     > RETURN                                                   null

End of function buildtree

Function printtree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 4, Position 2 = 7
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 26
Branch analysis from position: 8
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 24
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 24
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 22
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 22
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
Branch analysis from position: 26
Branch analysis from position: 7
filename:       /in/7rtJU
function name:  printTree
number of ops:  27
compiled vars:  !0 = $tree, !1 = $node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  107     0  E >   RECV                                             !0      
  109     1        TYPE_CHECK                                    2  ~2      !0
          2        BOOL_NOT                                         ~3      ~2
          3      > JMPZ_EX                                          ~3      ~3, ->7
          4    >   COUNT                                            ~4      !0
          5        IS_SMALLER                                       ~5      0, ~4
          6        BOOL                                             ~3      ~5
          7    > > JMPZ                                                     ~3, ->26
  110     8    >   ECHO                                                     '%3Cul%3E'
  111     9      > FE_RESET_R                                       $6      !0, ->24
         10    > > FE_FETCH_R                                               $6, !1, ->24
  113    11    >   FETCH_DIM_R                                      ~7      !1, 'data_title'
         12        CONCAT                                           ~8      '%3Cli%3E', ~7
         13        ECHO                                                     ~8
  114    14        ISSET_ISEMPTY_DIM_OBJ                         1  ~9      !1, 'children'
         15        BOOL_NOT                                         ~10     ~9
         16      > JMPZ                                                     ~10, ->22
  115    17    >   INIT_FCALL_BY_NAME                                       'printTree'
         18        CHECK_FUNC_ARG                                           
         19        FETCH_DIM_FUNC_ARG                               $11     !1, 'children'
         20        SEND_FUNC_ARG                                            $11
         21        DO_FCALL                                      0          
  117    22    >   ECHO                                                     '%3C%2Fli%3E'
  111    23      > JMP                                                      ->10
         24    >   FE_FREE                                                  $6
  119    25        ECHO                                                     '%3C%2Ful%3E'
  121    26    > > RETURN                                                   null

End of function printtree

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.67 ms | 1015 KiB | 15 Q