3v4l.org

run code in 300+ PHP versions simultaneously
<?php $menu = array( array(1,3,'Wurm 1.1', 2, 10), array(2,6,'Vogel 2.1', 2, 30), array(3,0,'Tiger 1', 1, 10), array(4,6,'Hund 2.2', 2, 40), array(5,3,'Katze 1.2', 2, 11), array(6,0,'Pferd 2', 1, 20), array(7,1,'Baer 1.1.1', 3, 0), array(8,3,'Schwein 1.3', 2, 12), array(9,4,'Esel 2.2.1', 3, 0), ); // Algorithmus hier /* steps: 1. sort items by levelId and sortId - finished 2. get max levelId value 'x' = iterations - ...that seems to be not the way to go. better way would be recursion for each element and search for children belonging to the current menuId.... 3. ... */ $result = array(); foreach ($menu as $key => $row){ $levelId[$key] = $row[3]; $sortId[$key] = $row[4]; } array_multisort($levelId, SORT_DESC, $sortId, SORT_DESC, $menu); $levels = $menu[0][3]; echo count($menu) . " items\n"; echo $levels . " levels\n"; function getChild($list){ foreach ($list as $entry){ if ($entry[1] == $list[0]){ array_push($entry, $target); } } } getChild($menu[8]); var_dump($result); $target = array( array(3,0,'Tiger 1', 1, 10), array(1,3,'Wurm 1.1', 2, 10), array(7,1,'Baer 1.1.1', 3, 0), array(5,3,'Katze 1.2', 2, 11), array(8,3,'Schwein 1.3', 2, 12), array(6,0,'Pferd 2', 1, 20), array(2,6,'Vogel 2.1', 2, 30), array(4,6,'Hund 2.2', 2, 40), array(9,4,'Esel 2.2.1', 3, 0), ); //var_dump($result == $target); //var_dump($result);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 12
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 12
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/99A1F
function name:  (null)
number of ops:  37
compiled vars:  !0 = $menu, !1 = $result, !2 = $row, !3 = $key, !4 = $levelId, !5 = $sortId, !6 = $levels, !7 = $target
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   26     1        ASSIGN                                                   !1, <array>
   28     2      > FE_RESET_R                                       $10     !0, ->12
          3    > > FE_FETCH_R                                       ~11     $10, !2, ->12
          4    >   ASSIGN                                                   !3, ~11
   29     5        FETCH_DIM_R                                      ~14     !2, 3
          6        ASSIGN_DIM                                               !4, !3
          7        OP_DATA                                                  ~14
   30     8        FETCH_DIM_R                                      ~16     !2, 4
          9        ASSIGN_DIM                                               !5, !3
         10        OP_DATA                                                  ~16
   28    11      > JMP                                                      ->3
         12    >   FE_FREE                                                  $10
   34    13        INIT_FCALL                                               'array_multisort'
         14        SEND_REF                                                 !4
         15        SEND_VAL                                                 3
         16        SEND_REF                                                 !5
         17        SEND_VAL                                                 3
         18        SEND_REF                                                 !0
         19        DO_ICALL                                                 
   36    20        FETCH_DIM_R                                      ~18     !0, 0
         21        FETCH_DIM_R                                      ~19     ~18, 3
         22        ASSIGN                                                   !6, ~19
   38    23        COUNT                                            ~21     !0
         24        CONCAT                                           ~22     ~21, '+items%0A'
         25        ECHO                                                     ~22
   39    26        CONCAT                                           ~23     !6, '+levels%0A'
         27        ECHO                                                     ~23
   54    28        INIT_FCALL                                               'getchild'
         29        FETCH_DIM_R                                      ~24     !0, 8
         30        SEND_VAL                                                 ~24
         31        DO_FCALL                                      0          
   56    32        INIT_FCALL                                               'var_dump'
         33        SEND_VAR                                                 !1
         34        DO_ICALL                                                 
   60    35        ASSIGN                                                   !7, <array>
   73    36      > RETURN                                                   1

Function getchild:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 12
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 12
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 11
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/99A1F
function name:  getChild
number of ops:  14
compiled vars:  !0 = $list, !1 = $entry, !2 = $target
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
   44     1      > FE_RESET_R                                       $3      !0, ->12
          2    > > FE_FETCH_R                                               $3, !1, ->12
   46     3    >   FETCH_DIM_R                                      ~4      !1, 1
          4        FETCH_DIM_R                                      ~5      !0, 0
          5        IS_EQUAL                                                 ~4, ~5
          6      > JMPZ                                                     ~6, ->11
   47     7    >   INIT_FCALL                                               'array_push'
          8        SEND_REF                                                 !1
          9        SEND_VAR                                                 !2
         10        DO_ICALL                                                 
   44    11    > > JMP                                                      ->2
         12    >   FE_FREE                                                  $3
   52    13      > RETURN                                                   null

End of function getchild

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
184.67 ms | 1403 KiB | 20 Q