PHP Recursive Function
- by Tempname
In my database I have a hierarchical flat table that returns data ordered by ParentID, ObjectID asc
I am having a bit of an issue getting this recursive function to work properly. I get the first ParentChildChild but after that I get nothing else.
Any help with this is greatly appreciated.
Here is my testing code:
$objectArr = array();
$objectData = DAOFactory::getTemplateObjectsDAO()->queryByTemplateID(1);
for($i = 0; $i < count($objectData); $i++)
{
if(empty($objectData[$i]->parentID))
{
echo $objectData[$i]->objectID;
$objectArr[$i] = $objectData[$i];
$objectArr[$i]->children = array();
$objectArr[$i]->children = getChildren($objectData[$i]->objectID, $objectData);
}
}
function getChildren($objectID, $data)
{
$childArr = array();
foreach($data as $object)
{
if($object->parentID == $objectID)
{
$childArr = $object;
$childArr->children = array();
$childArr->children = getChildren($object->objectID, $data);
}
}
return $childArr;
}
new dBug($objectData);
This is the output that I am getting:
Fullsize Link