PHP: Recursively get children of parent

狂风中的少年 提交于 2019-12-10 10:09:26

问题


I have a function which gets the ids of all children of a parent from my DB. So, if I looked up id 7, it might return an array with 5, 6 and 10. What I then want to do, is recursively find the children of those returned ids, and so on, to the final depth of the children.

I have tried to write a function to do this, but I am getting confused about recursion.

function getChildren($parent_id) {
    $tree = Array();
    $tree_string;
    if (!empty($parent_id)) {
        // getOneLevel() returns a one-dimentional array of child ids
        $tree = $this->getOneLevel($parent_id);
        foreach ($tree as $key => $val) {
            $ids = $this->getChildren($val);
            array_push($tree, $ids);
            //$tree[] = $this->getChildren($val);
            $tree_string .= implode(',', $tree);
        }

        return $tree_string;
    } else {
        return $tree;
    }

}//end getChildren()

After the function is run, I would like it to return a one-dimentional array of all the child ids that were found.


回答1:


This work fine for me:

function getOneLevel($catId){
    $query=mysql_query("SELECT categoryId FROM categories WHERE categoryMasterId='".$catId."'");
    $cat_id=array();
    if(mysql_num_rows($query)>0){
        while($result=mysql_fetch_assoc($query)){
            $cat_id[]=$result['categoryId'];
        }
    }   
    return $cat_id;
}

function getChildren($parent_id, $tree_string=array()) {
    $tree = array();
    // getOneLevel() returns a one-dimensional array of child ids        
    $tree = $this->getOneLevel($parent_id);     
    if(count($tree)>0 && is_array($tree)){      
        $tree_string=array_merge($tree_string,$tree);
    }
    foreach ($tree as $key => $val) {
        $this->getChildren($val, &$tree_string);
    }   
    return $tree_string;
}

Call the getChildren(yourid); Then it will return the complete array of children for that given node/parent.




回答2:


Nested Set Model instead of Adjacency List Model


Can I suggest that you store your nodes in your database under NSM instead of ALM?

Notice that with ALM, (which is what you are using) getting the children nodes is quite difficult, its possible, but requires extra work. If you use nested set model selecting a child node, or all nodes, or even finding the depth of all nodes can be done in a single SQL query.

I hope this sheds some light on how you could solve your problem, if you are still young in the development of your project switching now will save you a lot of headaches later.




回答3:


Rather than array_push($tree, $ids); try $tree = array_merge($tree, $ids);. Kill the $tree_string .= implode(',', $tree); and just return $tree. (Once)

function getChildren($parent_id) {
    $tree = Array();
    if (!empty($parent_id)) {
        $tree = $this->getOneLevel($parent_id);
        foreach ($tree as $key => $val) {
            $ids = $this->getChildren($val);
            a$tree = array_merge($tree, $ids);
        }
    }
    return $tree;
}


来源:https://stackoverflow.com/questions/2352689/php-recursively-get-children-of-parent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!