php递归函数,性能给力

function arPro($data,$res=array(),$pid='0',$level='0'){
  foreach ($data as $k => $v){
    if($v['comment_parent']==$pid){
      $res[$v['id']]['info']=$v;
      if($level!='0'){
        if($v['level']==$level){
          $child=null;
        }
        else{
          $child=arPro($data,array(),$v['comment_id'],$level+1);
        }
        $res[$v['id']]['child']=$child;
      }
      else{
        $child=arPro($data,array(),$v['comment_id']);
        if($child==''||$child==null){
            $res[$v['id']]['child']=null;
        }
        else{
            $res[$v['id']]['child']=$child;
        }
      }
    }
  }
  return $res;
}

  

原文地址:https://www.cnblogs.com/qiandu/p/4122533.html