码迷,mamicode.com
首页 > 其他好文 > 详细

实现无限极分类-递归实现

时间:2015-06-18 13:35:17      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

public function getTree(){
        $data = $this->select();
        $list = $this->_getTreeData($data);
        return $list;
    }
    private function _getTreeData($data=array(), $parent_id=0){
       $childs = $this->_buildTree($data, $parent_id); //获取所有的父分类
        if(empty($childs)){
            return null;
        }
        foreach($childs as $key => $val){
            $rescurTree = $this->_getTreeData($data, $val['id']); //递归获取当前分类下的所有子分类
            if($rescurTree != null){
                $childs[$key]['childs'] = $rescurTree; 
            }
        }
        return $childs;
    }
    private function _buildTree(&$data, $parent_id){
        $res = array();
        foreach($data as $key => $val){
            if($val['parent_id'] == $parent_id){
                $res[] = $val;
            }
        }
        return $res;
    }

实现无限极分类-递归实现

标签:

原文地址:http://blog.csdn.net/xueling022/article/details/46546871

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!