3v4l.org

run code in 500+ PHP versions simultaneously
<?php const IDX_ID = 0; const IDX_PARENT = 1; const IDX_SORT = 4; $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 function sortMenu($menu, $parentId = 0) { $items = array_filter($menu, function($item) use ($parentId) { return $item[IDX_PARENT] == $parentId; }); if (0 == count($items)) { return array(); } usort($items, function(array $a, array $b) { if ($a[IDX_SORT] == $b[IDX_SORT]) { return 0; } return $a[IDX_SORT] > $b[IDX_SORT] ? 1 : -1; }); $result = array(); foreach ($items as $item) { $result[] = $item; foreach (sortMenu($menu, $item[IDX_ID]) as $child) { $result[] = $child; } } return $result; } $result = sortMenu($menu); $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
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qPDZW
function name:  (null)
number of ops:  14
compiled vars:  !0 = $menu, !1 = $result, !2 = $target
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CONST                                                'IDX_ID', 0
    4     1        DECLARE_CONST                                                'IDX_PARENT', 1
    5     2        DECLARE_CONST                                                'IDX_SORT', 4
    7     3        ASSIGN                                                       !0, <array>
   44     4        INIT_FCALL                                                   'sortmenu'
          5        SEND_VAR                                                     !0
          6        DO_FCALL                                          0  $4      
          7        ASSIGN                                                       !1, $4
   46     8        ASSIGN                                                       !2, <array>
   58     9        INIT_FCALL                                                   'var_dump'
         10        IS_EQUAL                                             ~7      !1, !2
         11        SEND_VAL                                                     ~7
         12        DO_ICALL                                                     
         13      > RETURN                                                       1

Function sortmenu:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 13
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
2 jumps found. (Code = 77) Position 1 = 20, Position 2 = 37
Branch analysis from position: 20
2 jumps found. (Code = 78) Position 1 = 21, Position 2 = 37
Branch analysis from position: 21
2 jumps found. (Code = 77) Position 1 = 31, Position 2 = 35
Branch analysis from position: 31
2 jumps found. (Code = 78) Position 1 = 32, Position 2 = 35
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
Branch analysis from position: 35
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
filename:       /in/qPDZW
function name:  sortMenu
number of ops:  40
compiled vars:  !0 = $menu, !1 = $parentId, !2 = $items, !3 = $result, !4 = $item, !5 = $child
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   20     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      0
   22     2        INIT_FCALL                                                   'array_filter'
          3        SEND_VAR                                                     !0
          4        DECLARE_LAMBDA_FUNCTION                              ~6      [0]
          5        BIND_LEXICAL                                                 ~6, !1
   24     6        SEND_VAL                                                     ~6
   22     7        DO_ICALL                                             $7      
          8        ASSIGN                                                       !2, $7
   25     9        COUNT                                                ~9      !2
         10        IS_EQUAL                                                     ~9, 0
         11      > JMPZ                                                         ~10, ->13
   26    12    > > RETURN                                                       <array>
   28    13    >   INIT_FCALL                                                   'usort'
         14        SEND_REF                                                     !2
         15        DECLARE_LAMBDA_FUNCTION                              ~11     [1]
   33    16        SEND_VAL                                                     ~11
   28    17        DO_ICALL                                                     
   34    18        ASSIGN                                                       !3, <array>
   35    19      > FE_RESET_R                                           $14     !2, ->37
         20    > > FE_FETCH_R                                                   $14, !4, ->37
   36    21    >   ASSIGN_DIM                                                   !3
         22        OP_DATA                                                      !4
   37    23        INIT_FCALL_BY_NAME                                           'sortMenu'
         24        SEND_VAR_EX                                                  !0
         25        CHECK_FUNC_ARG                                               
         26        FETCH_CONSTANT                                       ~16     'IDX_ID'
         27        FETCH_DIM_FUNC_ARG                                   $17     !4, ~16
         28        SEND_FUNC_ARG                                                $17
         29        DO_FCALL                                          0  $18     
         30      > FE_RESET_R                                           $19     $18, ->35
         31    > > FE_FETCH_R                                                   $19, !5, ->35
   38    32    >   ASSIGN_DIM                                                   !3
         33        OP_DATA                                                      !5
   37    34      > JMP                                                          ->31
         35    >   FE_FREE                                                      $19
   35    36      > JMP                                                          ->20
         37    >   FE_FREE                                                      $14
   41    38      > RETURN                                                       !3
   42    39*     > 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/qPDZW
function name:  {closure:sortMenu():22}
number of ops:  7
compiled vars:  !0 = $item, !1 = $parentId
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   22     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
   23     2        FETCH_CONSTANT                                       ~2      'IDX_PARENT'
          3        FETCH_DIM_R                                          ~3      !0, ~2
          4        IS_EQUAL                                             ~4      !1, ~3
          5      > RETURN                                                       ~4
   24     6*     > RETURN                                                       null

End of Dynamic Function 0

Dynamic Function 1
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 9
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 17
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qPDZW
function name:  {closure:sortMenu():28}
number of ops:  20
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   28     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   29     2        FETCH_CONSTANT                                       ~2      'IDX_SORT'
          3        FETCH_DIM_R                                          ~3      !0, ~2
          4        FETCH_CONSTANT                                       ~4      'IDX_SORT'
          5        FETCH_DIM_R                                          ~5      !1, ~4
          6        IS_EQUAL                                                     ~3, ~5
          7      > JMPZ                                                         ~6, ->9
   30     8    > > RETURN                                                       0
   32     9    >   FETCH_CONSTANT                                       ~7      'IDX_SORT'
         10        FETCH_DIM_R                                          ~8      !0, ~7
         11        FETCH_CONSTANT                                       ~9      'IDX_SORT'
         12        FETCH_DIM_R                                          ~10     !1, ~9
         13        IS_SMALLER                                                   ~10, ~8
         14      > JMPZ                                                         ~11, ->17
         15    >   QM_ASSIGN                                            ~12     1
         16      > JMP                                                          ->18
         17    >   QM_ASSIGN                                            ~12     -1
         18    > > RETURN                                                       ~12
   33    19*     > RETURN                                                       null

End of Dynamic Function 1

End of function sortmenu

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
163.67 ms | 2737 KiB | 17 Q