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; } }
循环调用getchild($pid)
这个递归的时候,每条父评论下的子评论会把前一条父评论的子评论包含进来,这个怎么解决啊?
我知道是由于static $arr;
静态变量的原因。但是由于业务需要我需要取出这样的数据结构,就是做一个类似本站的回复的需求。把每条父评论下所有的子评论取出来。但是上面的做法,出现了,下一条父评论的子评论包含了上一条的子评论数据。
有什么办法可以解决这个问题!!!
又是递归又是static有点晕。
改成
这样不行吗