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. ... */ const IDX_ID = 0; const IDX_PARENT = 1; const IDX_SORT = 4; $result = array(); foreach ($menu as $key => $row){ $levelId[$key] = $row[3]; $sortId[$key] = $row[4]; } array_multisort($levelId, SORT_ASC, $sortId, SORT_ASC, $menu); $parentId = 4; $items = array_filter($menu, function($item) use ($parentId) { return $item[IDX_PARENT] == $parentId; }); var_dump($items); $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);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 15
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 15
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/TThap
function name:  (null)
number of ops:  36
compiled vars:  !0 = $menu, !1 = $result, !2 = $row, !3 = $key, !4 = $levelId, !5 = $sortId, !6 = $parentId, !7 = $items, !8 = $target
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   27     1        DECLARE_CONST                                            'IDX_ID', 0
   28     2        DECLARE_CONST                                            'IDX_PARENT', 1
   29     3        DECLARE_CONST                                            'IDX_SORT', 4
   32     4        ASSIGN                                                   !1, <array>
   34     5      > FE_RESET_R                                       $11     !0, ->15
          6    > > FE_FETCH_R                                       ~12     $11, !2, ->15
          7    >   ASSIGN                                                   !3, ~12
   35     8        FETCH_DIM_R                                      ~15     !2, 3
          9        ASSIGN_DIM                                               !4, !3
         10        OP_DATA                                                  ~15
   36    11        FETCH_DIM_R                                      ~17     !2, 4
         12        ASSIGN_DIM                                               !5, !3
         13        OP_DATA                                                  ~17
   34    14      > JMP                                                      ->6
         15    >   FE_FREE                                                  $11
   39    16        INIT_FCALL                                               'array_multisort'
         17        SEND_REF                                                 !4
         18        SEND_VAL                                                 4
         19        SEND_REF                                                 !5
         20        SEND_VAL                                                 4
         21        SEND_REF                                                 !0
         22        DO_ICALL                                                 
   41    23        ASSIGN                                                   !6, 4
   42    24        INIT_FCALL                                               'array_filter'
         25        SEND_VAR                                                 !0
         26        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FTThap%3A42%240'
         27        BIND_LEXICAL                                             ~20, !6
   44    28        SEND_VAL                                                 ~20
         29        DO_ICALL                                         $21     
   42    30        ASSIGN                                                   !7, $21
   46    31        INIT_FCALL                                               'var_dump'
         32        SEND_VAR                                                 !7
         33        DO_ICALL                                                 
   48    34        ASSIGN                                                   !8, <array>
   60    35      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FTThap%3A42%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TThap
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $item, !1 = $parentId
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
   43     2        FETCH_CONSTANT                                   ~2      'IDX_PARENT'
          3        FETCH_DIM_R                                      ~3      !0, ~2
          4        IS_EQUAL                                         ~4      !1, ~3
          5      > RETURN                                                   ~4
   44     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FTThap%3A42%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
199.56 ms | 1400 KiB | 19 Q