3v4l.org

run code in 200+ 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 $result = array(); function flatten($arr) { global $result; foreach($arr as $r) { if(isset($r["r"])) { $x = $r["r"]; unset($r["r"]); $result[] = $r; flatten($x); } else { $result[] = $r; } } } $tmp = array(); foreach($menu as &$m) { $id = $m[0]; $ref = $m[1]; $m["r"] = array_filter($menu, function($item) use ($id) { return $item[1] == $id; }); if(sizeof($m["r"]) == 0) unset($m["r"]); if($ref == 0) { $tmp[] = $m; } } flatten($tmp); $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);
based on PTuRc
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 4, Position 2 = 27
Branch analysis from position: 4
2 jumps found. (Code = 126) Position 1 = 5, Position 2 = 27
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 22
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 26
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 26
Branch analysis from position: 22
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
filename:       /in/RuFPp
function name:  (null)
number of ops:  37
compiled vars:  !0 = $menu, !1 = $result, !2 = $tmp, !3 = $m, !4 = $id, !5 = $ref, !6 = $target
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   4     0  E >   QM_ASSIGN                                                !0
  17     1        QM_ASSIGN                                                !1
  35     2        QM_ASSIGN                                                !2
  37     3      > FE_RESET_RW                                      $10     !0, ->27
         4    > > FE_FETCH_RW                                              $10, !3, ->27
  38     5    >   FETCH_DIM_R                                      ~11     !3, 0
         6        QM_ASSIGN                                                !4
  39     7        FETCH_DIM_R                                      ~13     !3, 1
         8        QM_ASSIGN                                                !5
  40     9        INIT_FCALL                                               'array_filter'
        10        SEND_VAR                                                 !0
        11        RAISE_ABSTRACT_ERROR                             ~16     '%00%7Bclosure%7D%2Fin%2FRuFPp0x7f41ba87a351'
        12        BIND_LEXICAL                                             ~16, !4
  42    13        SEND_VAL                                                 ~16
        14        DO_ICALL                                         $17     
  40    15        ASSIGN_ADD                                    0          !3, 'r'
  42    16        OP_DATA                                                  $17
  44    17        FETCH_DIM_R                                      ~18     !3, 'r'
        18        COUNT                                            ~19     ~18
        19        IS_NOT_EQUAL                                     ~20     ~19, 0
        20      > JMPZ                                                     ~20, ->22
        21    >   UNSET_DIM                                                !3, 'r'
  46    22    >   IS_NOT_EQUAL                                     ~21     !5, 0
        23      > JMPZ                                                     ~21, ->26
  47    24    >   ASSIGN_ADD                                    0          !2
        25        OP_DATA                                                  !3
  37    26    > > JMP                                                      ->4
        27    >   FE_FREE                                                  $10
  51    28        INIT_FCALL                                               'flatten'
        29        SEND_VAR                                                 !2
        30        DO_FCALL                                      0          
  55    31        QM_ASSIGN                                                !6
  66    32        INIT_FCALL                                               'var_dump'
        33        IS_NOT_EQUAL                                     ~25     !1, !6
        34        SEND_VAL                                                 ~25
        35        DO_ICALL                                                 
        36      > RETURN                                                   1

Function flatten:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 18
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 18
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 15
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 17
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/RuFPp
function name:  flatten
number of ops:  20
compiled vars:  !0 = $arr, !1 = $result, !2 = $r, !3 = $x
line     #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  19     0  E >   RECV                                             !0      
  20     1        BIND_GLOBAL                                              !1, 'result'
  21     2      > FE_RESET_R                                       $4      !0, ->18
         3    > > FE_FETCH_R                                               $4, !2, ->18
  22     4    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~5      !2, 'r'
         5      > JMPZ                                                     ~5, ->15
  23     6    >   FETCH_DIM_R                                      ~6      !2, 'r'
         7        QM_ASSIGN                                                !3
  24     8        UNSET_DIM                                                !2, 'r'
  25     9        ASSIGN_ADD                                    0          !1
        10        OP_DATA                                                  !2
  26    11        INIT_FCALL_BY_NAME                                       'flatten'
        12        SEND_VAR_EX                                              !3
        13        DO_FCALL                                      0          
        14      > JMP                                                      ->17
  28    15    >   ASSIGN_ADD                                    0          !1
        16        OP_DATA                                                  !2
  21    17    > > JMP                                                      ->3
        18    >   FE_FREE                                                  $4
  32    19      > RETURN                                                   null

End of function flatten

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

End of function %00%7Bclosure%7D%2Fin%2FRuFPp0x7f41ba87a351

Generated using Vulcan Logic Dumper, using php 7.4.0