javascript - How to solve the problem that when a for loop calls a recursive function, the data contains the previous data record? ?
某草草
某草草 2017-07-04 13:46:06
0
1
811
function getchild($pid) {
    static $arr;
    $list = "select * from table where parent_id = $pid";
    foreach($list as $k =>$v) {
        $arr[] = $list[$k];
        getchild($v['comment_id']);
    }
    return $arr;
}

function get_comment_list() {
    $comment_list = "select * table where parent_id=0";
    foreach($comment_list as $k =>$v) {
        $child_comments = getchild($v['comment_id']); //递归取出父评论下所有的子评论
        $comment_list[$k]['childs'] = $child_comments;
    }
}

Loop callgetchild($pid)During this recursion, the child comments under each parent comment will include the child comments of the previous parent comment. How to solve this?

I know it is due to static $arr; static variables. But due to business needs, I need to take out such a data structure, which is the need to make a reply similar to this site. Get all child comments under each parent comment. But with the above approach, the child comment of the next parent comment contains the child comment data of the previous one.

Is there any way to solve this problem! ! !

某草草
某草草

reply all(1)
阿神

It’s recursive and static, which is a bit confusing.

$arr[] = $list[$k];
getchild($v['comment_id]);

changed to

$arr[$v['comment_id]] = getchild($v['comment_id]);

Isn’t this okay?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template