3v4l.org

run code in 300+ PHP versions simultaneously
<?php $menu = array( array(7,1,'Baer 1.1.1', 3, 0), array(9,4,'Esel 2.2.1', 3, 0), array(1,3,'Wurm 1.1', 2, 10), array(3,0,'Tiger 1', 1, 10), array(5,3,'Katze 1.2', 2, 11), array(8,3,'Schwein 1.3', 2, 12), array(2,6,'Vogel 2.1', 2, 30), array(4,6,'Hund 2.2', 2, 40), array(6,0,'Pferd 2', 1, 20), ); // Algorithmus hier /** * This Class represents a single folder Node * * @package wbsFramework * @subpackage * @author -=WBS=- * Wolfgang Blessen Software * www.blessen.de * @copyright private * @version 2.5 * @since 13.05.15 */ class FolderNode { /** * @var int */ private $ID; /** * @var string */ private $Name; /** * @var int */ private $Level; /** * @var array Array of {FolderNode} */ private $Children; /** * @var FolderNode | null */ private $Parent; /** * Create a new Node * * @param int $ID * @param string $Name */ public function __construct($ID,$Name) { #shout('new FolderNode '.$ID.': '.$Name); $this->ID = (int)$ID; $this->Name = $Name; $this->Children = array(); $this->Parent = null; } /** * @return int */ public function getID() { return $this->ID; } /** * @param int $ID */ public function setID($ID) { $this->ID = (int)$ID; } /** * @return string */ public function getName() { return $this->Name; } /** * @param string $Name */ public function setName($Name) { $this->Name = $Name; } /** * @return int */ public function getLevel() { return $this->Level; } /** * @param int $Level */ public function setLevel($Level) { $this->Level = (int)$Level; } /** * @return FolderNode[] Array of All Children */ public function getChildren() { return $this->Children; } /** * @return boolean Do we have children */ public function hasChildren() { return count($this->Children) ? true : false; } /** * @param FolderNode $Child */ public function addChild(FolderNode $Child) { $this->Children[] = $Child; } /** * @return FolderNode|null */ public function getParent() { return $this->Parent; } /** * @return boolean */ public function hasParent() { return ($this->Parent !== null); } /** * @param FolderNode|null $Parent */ public function setParent($Parent) { $this->Parent = $Parent; } } /** * @param FolderNode $FolderNode * @param array $FolderList * @param int $current_level * * @return bool */ function findChildren($FolderNode,$FolderList, $current_level){ $myID = $FolderNode->getID(); $found_child =false; foreach((array)$FolderList as $folder) { if($folder[1] == $myID){ //Setup a new Node $child_node = new FolderNode($folder[0],$folder[2]); $child_node->setLevel($current_level); $child_node->setParent($FolderNode); findChildren($child_node,$FolderList,$current_level+1); $FolderNode->addChild($child_node); //Find the Children $found_child = true; } } return $found_child; } $menu_by_id = []; foreach($menu as $item){ $menu_by_id[$item[0]] = $item; } // Set the Root Level $current_level = 0; $root_node = new FolderNode(0,'Root'); $root_node->setLevel($current_level); $root_node->setParent(null); findChildren($root_node,$menu,$current_level +1); $result = array(); foreach($root_node->getChildren() as $the_child){ $i =0; $result[] = $menu_by_id[$the_child->getID()]; if($the_child->hasChildren()){ foreach($the_child->getChildren() as $the_grand_child){ $result[] = $menu_by_id[$the_grand_child->getID()]; foreach($the_grand_child->getChildren() as $the_grand_grand_child){ $result[] = $menu_by_id[$the_grand_grand_child->getID()]; } } } } //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);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 8
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 8
2 jumps found. (Code = 77) Position 1 = 31, Position 2 = 64
Branch analysis from position: 31
2 jumps found. (Code = 78) Position 1 = 32, Position 2 = 64
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 63
Branch analysis from position: 41
2 jumps found. (Code = 77) Position 1 = 44, Position 2 = 62
Branch analysis from position: 44
2 jumps found. (Code = 78) Position 1 = 45, Position 2 = 62
Branch analysis from position: 45
2 jumps found. (Code = 77) Position 1 = 53, Position 2 = 60
Branch analysis from position: 53
2 jumps found. (Code = 78) Position 1 = 54, Position 2 = 60
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 53
Branch analysis from position: 53
Branch analysis from position: 60
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
Branch analysis from position: 60
Branch analysis from position: 62
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
Branch analysis from position: 62
Branch analysis from position: 63
Branch analysis from position: 64
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 64
Branch analysis from position: 8
filename:       /in/B3SAD
function name:  (null)
number of ops:  71
compiled vars:  !0 = $menu, !1 = $menu_by_id, !2 = $item, !3 = $current_level, !4 = $root_node, !5 = $result, !6 = $the_child, !7 = $i, !8 = $the_grand_child, !9 = $the_grand_grand_child, !10 = $target
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
  189     1        ASSIGN                                                   !1, <array>
  190     2      > FE_RESET_R                                       $13     !0, ->8
          3    > > FE_FETCH_R                                               $13, !2, ->8
  191     4    >   FETCH_DIM_R                                      ~14     !2, 0
          5        ASSIGN_DIM                                               !1, ~14
          6        OP_DATA                                                  !2
  190     7      > JMP                                                      ->3
          8    >   FE_FREE                                                  $13
  195     9        ASSIGN                                                   !3, 0
  197    10        NEW                                              $17     'FolderNode'
         11        SEND_VAL_EX                                              0
         12        SEND_VAL_EX                                              'Root'
         13        DO_FCALL                                      0          
         14        ASSIGN                                                   !4, $17
  199    15        INIT_METHOD_CALL                                         !4, 'setLevel'
         16        SEND_VAR_EX                                              !3
         17        DO_FCALL                                      0          
  200    18        INIT_METHOD_CALL                                         !4, 'setParent'
         19        SEND_VAL_EX                                              null
         20        DO_FCALL                                      0          
  202    21        INIT_FCALL                                               'findchildren'
         22        SEND_VAR                                                 !4
         23        SEND_VAR                                                 !0
         24        ADD                                              ~22     !3, 1
         25        SEND_VAL                                                 ~22
         26        DO_FCALL                                      0          
  205    27        ASSIGN                                                   !5, <array>
  206    28        INIT_METHOD_CALL                                         !4, 'getChildren'
         29        DO_FCALL                                      0  $25     
         30      > FE_RESET_R                                       $26     $25, ->64
         31    > > FE_FETCH_R                                               $26, !6, ->64
  207    32    >   ASSIGN                                                   !7, 0
  208    33        INIT_METHOD_CALL                                         !6, 'getID'
         34        DO_FCALL                                      0  $29     
         35        FETCH_DIM_R                                      ~30     !1, $29
         36        ASSIGN_DIM                                               !5
         37        OP_DATA                                                  ~30
  209    38        INIT_METHOD_CALL                                         !6, 'hasChildren'
         39        DO_FCALL                                      0  $31     
         40      > JMPZ                                                     $31, ->63
  210    41    >   INIT_METHOD_CALL                                         !6, 'getChildren'
         42        DO_FCALL                                      0  $32     
         43      > FE_RESET_R                                       $33     $32, ->62
         44    > > FE_FETCH_R                                               $33, !8, ->62
  211    45    >   INIT_METHOD_CALL                                         !8, 'getID'
         46        DO_FCALL                                      0  $35     
         47        FETCH_DIM_R                                      ~36     !1, $35
         48        ASSIGN_DIM                                               !5
         49        OP_DATA                                                  ~36
  212    50        INIT_METHOD_CALL                                         !8, 'getChildren'
         51        DO_FCALL                                      0  $37     
         52      > FE_RESET_R                                       $38     $37, ->60
         53    > > FE_FETCH_R                                               $38, !9, ->60
  213    54    >   INIT_METHOD_CALL                                         !9, 'getID'
         55        DO_FCALL                                      0  $40     
         56        FETCH_DIM_R                                      ~41     !1, $40
         57        ASSIGN_DIM                                               !5
         58        OP_DATA                                                  ~41
  212    59      > JMP                                                      ->53
         60    >   FE_FREE                                                  $38
  210    61      > JMP                                                      ->44
         62    >   FE_FREE                                                  $33
  206    63    > > JMP                                                      ->31
         64    >   FE_FREE                                                  $26
  220    65        ASSIGN                                                   !10, <array>
  232    66        INIT_FCALL                                               'var_dump'
         67        IS_EQUAL                                         ~43     !5, !10
         68        SEND_VAL                                                 ~43
         69        DO_ICALL                                                 
         70      > RETURN                                                   1

Function findchildren:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 39
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 39
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 38
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 38
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
filename:       /in/B3SAD
function name:  findChildren
number of ops:  42
compiled vars:  !0 = $FolderNode, !1 = $FolderList, !2 = $current_level, !3 = $myID, !4 = $found_child, !5 = $folder, !6 = $child_node
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  169     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
  171     3        INIT_METHOD_CALL                                         !0, 'getID'
          4        DO_FCALL                                      0  $7      
          5        ASSIGN                                                   !3, $7
  172     6        ASSIGN                                                   !4, <false>
  174     7        CAST                                          7  ~10     !1
          8      > FE_RESET_R                                       $11     ~10, ->39
          9    > > FE_FETCH_R                                               $11, !5, ->39
  175    10    >   FETCH_DIM_R                                      ~12     !5, 1
         11        IS_EQUAL                                                 !3, ~12
         12      > JMPZ                                                     ~13, ->38
  177    13    >   NEW                                              $14     'FolderNode'
         14        CHECK_FUNC_ARG                                           
         15        FETCH_DIM_FUNC_ARG                               $15     !5, 0
         16        SEND_FUNC_ARG                                            $15
         17        CHECK_FUNC_ARG                                           
         18        FETCH_DIM_FUNC_ARG                               $16     !5, 2
         19        SEND_FUNC_ARG                                            $16
         20        DO_FCALL                                      0          
         21        ASSIGN                                                   !6, $14
  178    22        INIT_METHOD_CALL                                         !6, 'setLevel'
         23        SEND_VAR_EX                                              !2
         24        DO_FCALL                                      0          
  179    25        INIT_METHOD_CALL                                         !6, 'setParent'
         26        SEND_VAR_EX                                              !0
         27        DO_FCALL                                      0          
  180    28        INIT_FCALL_BY_NAME                                       'findChildren'
         29        SEND_VAR_EX                                              !6
         30        SEND_VAR_EX                                              !1
         31        ADD                                              ~21     !2, 1
         32        SEND_VAL_EX                                              ~21
         33        DO_FCALL                                      0          
  181    34        INIT_METHOD_CALL                                         !0, 'addChild'
         35        SEND_VAR_EX                                              !6
         36        DO_FCALL                                      0          
  183    37        ASSIGN                                                   !4, <true>
  174    38    > > JMP                                                      ->9
         39    >   FE_FREE                                                  $11
  186    40      > RETURN                                                   !4
  187    41*     > RETURN                                                   null

End of function findchildren

Class FolderNode:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B3SAD
function name:  __construct
number of ops:  12
compiled vars:  !0 = $ID, !1 = $Name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   61     2        CAST                                          4  ~3      !0
          3        ASSIGN_OBJ                                               'ID'
          4        OP_DATA                                                  ~3
   62     5        ASSIGN_OBJ                                               'Name'
          6        OP_DATA                                                  !1
   63     7        ASSIGN_OBJ                                               'Children'
          8        OP_DATA                                                  <array>
   64     9        ASSIGN_OBJ                                               'Parent'
         10        OP_DATA                                                  null
   65    11      > RETURN                                                   null

End of function __construct

Function getid:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B3SAD
function name:  getID
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   FETCH_OBJ_R                                      ~0      'ID'
          1      > RETURN                                                   ~0
   73     2*     > RETURN                                                   null

End of function getid

Function setid:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B3SAD
function name:  setID
number of ops:  5
compiled vars:  !0 = $ID
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   78     0  E >   RECV                                             !0      
   80     1        CAST                                          4  ~2      !0
          2        ASSIGN_OBJ                                               'ID'
          3        OP_DATA                                                  ~2
   81     4      > RETURN                                                   null

End of function setid

Function getname:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B3SAD
function name:  getName
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   88     0  E >   FETCH_OBJ_R                                      ~0      'Name'
          1      > RETURN                                                   ~0
   89     2*     > RETURN                                                   null

End of function getname

Function setname:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B3SAD
function name:  setName
number of ops:  4
compiled vars:  !0 = $Name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   94     0  E >   RECV                                             !0      
   96     1        ASSIGN_OBJ                                               'Name'
          2        OP_DATA                                                  !0
   97     3      > RETURN                                                   null

End of function setname

Function getlevel:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B3SAD
function name:  getLevel
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  104     0  E >   FETCH_OBJ_R                                      ~0      'Level'
          1      > RETURN                                                   ~0
  105     2*     > RETURN                                                   null

End of function getlevel

Function setlevel:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B3SAD
function name:  setLevel
number of ops:  5
compiled vars:  !0 = $Level
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  110     0  E >   RECV                                             !0      
  112     1        CAST                                          4  ~2      !0
          2        ASSIGN_OBJ                                               'Level'
          3        OP_DATA                                                  ~2
  113     4      > RETURN                                                   null

End of function setlevel

Function getchildren:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B3SAD
function name:  getChildren
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  120     0  E >   FETCH_OBJ_R                                      ~0      'Children'
          1      > RETURN                                                   ~0
  121     2*     > RETURN                                                   null

End of function getchildren

Function haschildren:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B3SAD
function name:  hasChildren
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  127     0  E >   FETCH_OBJ_R                                      ~0      'Children'
          1        COUNT                                            ~1      ~0
          2      > JMPZ                                                     ~1, ->5
          3    >   QM_ASSIGN                                        ~2      <true>
          4      > JMP                                                      ->6
          5    >   QM_ASSIGN                                        ~2      <false>
          6    > > RETURN                                                   ~2
  128     7*     > RETURN                                                   null

End of function haschildren

Function addchild:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B3SAD
function name:  addChild
number of ops:  5
compiled vars:  !0 = $Child
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  133     0  E >   RECV                                             !0      
  135     1        FETCH_OBJ_W                                      $1      'Children'
          2        ASSIGN_DIM                                               $1
          3        OP_DATA                                                  !0
  136     4      > RETURN                                                   null

End of function addchild

Function getparent:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B3SAD
function name:  getParent
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  143     0  E >   FETCH_OBJ_R                                      ~0      'Parent'
          1      > RETURN                                                   ~0
  144     2*     > RETURN                                                   null

End of function getparent

Function hasparent:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B3SAD
function name:  hasParent
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  151     0  E >   FETCH_OBJ_R                                      ~0      'Parent'
          1        TYPE_CHECK                                  1020  ~1      ~0
          2      > RETURN                                                   ~1
  152     3*     > RETURN                                                   null

End of function hasparent

Function setparent:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/B3SAD
function name:  setParent
number of ops:  4
compiled vars:  !0 = $Parent
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  157     0  E >   RECV                                             !0      
  159     1        ASSIGN_OBJ                                               'Parent'
          2        OP_DATA                                                  !0
  160     3      > RETURN                                                   null

End of function setparent

End of class FolderNode.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
242.09 ms | 1090 KiB | 15 Q