<?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);
preferences:
25.41 ms | 404 KiB | 5 Q