3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = array( [ "name" => "cat1", "id" => "1", "parent" => "", ], [ "name" => "cat2", "id" => "2", "parent" => "", ], [ "name" => "subcat1", "id" => "6", "parent" => "1", ], [ "name" => "subsubcat1", "id" => "7", "parent" => "6", ], [ "name" => "subcat2", "id" => "5", "parent" => "2", ], ); function list_item($arr, $item) { echo "<li>{$item['name']}</li>\n"; // find any children $children = array_filter($arr, function ($i) use ($item) { return $i['parent'] == $item['id']; }); if (!empty($children)) { echo "<ul>\n"; foreach ($children as $child) { list_item($arr, $child); } echo "</ul>\n"; } } $parents = array_filter($arr, function ($item) { return !$item['parent'];}); echo "<ul>\n"; foreach ($parents as $parent) { list_item($arr, $parent); } echo "</ul>\n";
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 15
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 15
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/qOvd0
function name:  (null)
number of ops:  18
compiled vars:  !0 = $arr, !1 = $parents, !2 = $parent
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   44     1        INIT_FCALL                                               'array_filter'
          2        SEND_VAR                                                 !0
          3        DECLARE_LAMBDA_FUNCTION                          ~4      [0]
          4        SEND_VAL                                                 ~4
          5        DO_ICALL                                         $5      
          6        ASSIGN                                                   !1, $5
   45     7        ECHO                                                     '%3Cul%3E%0A'
   46     8      > FE_RESET_R                                       $7      !1, ->15
          9    > > FE_FETCH_R                                               $7, !2, ->15
   47    10    >   INIT_FCALL                                               'list_item'
         11        SEND_VAR                                                 !0
         12        SEND_VAR                                                 !2
         13        DO_FCALL                                      0          
   46    14      > JMP                                                      ->9
         15    >   FE_FREE                                                  $7
   49    16        ECHO                                                     '%3C%2Ful%3E%0A'
         17      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qOvd0
function name:  {closure}
number of ops:  5
compiled vars:  !0 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
          1        FETCH_DIM_R                                      ~1      !0, 'parent'
          2        BOOL_NOT                                         ~2      ~1
          3      > RETURN                                                   ~2
          4*     > RETURN                                                   null

End of Dynamic Function 0

Function list_item:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 27
Branch analysis from position: 17
2 jumps found. (Code = 77) Position 1 = 19, Position 2 = 25
Branch analysis from position: 19
2 jumps found. (Code = 78) Position 1 = 20, Position 2 = 25
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
Branch analysis from position: 27
filename:       /in/qOvd0
function name:  list_item
number of ops:  28
compiled vars:  !0 = $arr, !1 = $item, !2 = $children, !3 = $child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   32     2        ROPE_INIT                                     3  ~6      '%3Cli%3E'
          3        FETCH_DIM_R                                      ~4      !1, 'name'
          4        ROPE_ADD                                      1  ~6      ~6, ~4
          5        ROPE_END                                      2  ~5      ~6, '%3C%2Fli%3E%0A'
          6        ECHO                                                     ~5
   34     7        INIT_FCALL                                               'array_filter'
          8        SEND_VAR                                                 !0
          9        DECLARE_LAMBDA_FUNCTION                          ~8      [0]
         10        BIND_LEXICAL                                             ~8, !1
         11        SEND_VAL                                                 ~8
         12        DO_ICALL                                         $9      
         13        ASSIGN                                                   !2, $9
   35    14        ISSET_ISEMPTY_CV                                 ~11     !2
         15        BOOL_NOT                                         ~12     ~11
         16      > JMPZ                                                     ~12, ->27
   36    17    >   ECHO                                                     '%3Cul%3E%0A'
   37    18      > FE_RESET_R                                       $13     !2, ->25
         19    > > FE_FETCH_R                                               $13, !3, ->25
   38    20    >   INIT_FCALL_BY_NAME                                       'list_item'
         21        SEND_VAR_EX                                              !0
         22        SEND_VAR_EX                                              !3
         23        DO_FCALL                                      0          
   37    24      > JMP                                                      ->19
         25    >   FE_FREE                                                  $13
   40    26        ECHO                                                     '%3C%2Ful%3E%0A'
   42    27    > > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qOvd0
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $i, !1 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        FETCH_DIM_R                                      ~2      !0, 'parent'
          3        FETCH_DIM_R                                      ~3      !1, 'id'
          4        IS_EQUAL                                         ~4      ~2, ~3
          5      > RETURN                                                   ~4
          6*     > RETURN                                                   null

End of Dynamic Function 0

End of function list_item

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
165.83 ms | 1450 KiB | 15 Q