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);
Output for 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
bool(true)
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/B3SAD on line 189
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/B3SAD on line 189
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/B3SAD on line 34
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/B3SAD on line 34
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/B3SAD on line 34
Process exited with code 255.

preferences:
151.55 ms | 412 KiB | 5 Q