开发者

PHP Recursive Function

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 Parent>Child>Child but after that I get nothing else.

Any help with this is greatly app开发者_如何学Goreciated.

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

PHP Recursive Function


Here is a small cross section of my data:

ObjectID     ParentID
1   
3           1
4           3
10          3
11          4
12          4
16          7
15          11
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜