douti0687 2010-06-07 08:39
浏览 14

PHP递归函数

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 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

alt text

  • 写回答

1条回答 默认 最新

  • dreamact3026 2010-06-07 09:37
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致