php递归函数,性能给力

时间:2014-11-26 10:52:38   收藏:0   阅读:223
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;
}

  

原文:http://www.cnblogs.com/qiandu/p/4122533.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!