PHP实现无限极分类

PHP实现无限极分类

说到无限极分类,比较常见的做法是在建表的时候,增加一个PID字段用来区别自己所属的分类 由于展示数据的时候,需要表达出这种所属关系,所以必然要在读取数据的时候进行一系列处理,由此就牵涉到了两种算法 递归和引用,递归没什么好说的很简单重复调用自己就行了

引用算法

先贴上代码

$array = array(
    array('id' => 6, 'pid' => 2, 'name' => '徐汇区'),
    array('id' => 7, 'pid' => 6, 'name' => '徐家汇'),
    array('id' => 8, 'pid' => 2, 'name' => '黄浦区'),
    array('id' => 9, 'pid' => 2, 'name' => '普陀区'),
    array('id' => 10, 'pid' => 9, 'name' => '曹杨路'),
    array('id' => 1, 'pid' => 0, 'name' => '北京市'),
    array('id' => 2, 'pid' => 0, 'name' => '上海市'),
    array('id' => 3, 'pid' => 1, 'name' => '东城区'),
    array('id' => 4, 'pid' => 1, 'name' => '朝阳区'),
    array('id' => 5, 'pid' => 1, 'name' => '海淀区'),
);
function getTree($data){
   $items=array();
   foreach($data as $item){
       $items[$item['id']]=$item;
   }
   $tree=array();
    foreach($items as $k => $v){
        if(isset($items[$v['pid']])){
            $items[$v['pid']]['son'][]=&$items[$k];
        }else{
            $tree[]=&$items[$k];
        }
    }
    return $tree;
}
echo "<pre>";print_r(getTree($array));echo "<pre>";

这里使用的很巧妙,可以看成3个步骤:

一.

 foreach($data as $item){
       $items[$item['id']]=$item;
   }
组装数组,分类的首要基础就是ID和后面的PID关联的,让数组的键值具有意义    

二.

$items[$v['pid']]['son'][]=&$items[$k];
利用键值和PID的关联性巧妙利用了引用实现了给当前数组赋值改变了结构,看数组结构

Array
(
    [6] => Array
        (
            [id] => 6
            [pid] => 2
            [name] => 徐汇区
            [son] => Array
                (
                    [0] => Array
                        (
                            [id] => 7
                            [pid] => 6
                            [name] => 徐家汇
                        )
                )
        )
    [7] => Array
        (
            [id] => 7
            [pid] => 6
            [name] => 徐家汇
        )
    [8] => Array
        (
            [id] => 8
            [pid] => 2
            [name] => 黄浦区
        )
    [9] => Array
        (
            [id] => 9
            [pid] => 2
            [name] => 普陀区
            [son] => Array
                (
                    [0] => Array
                        (
                            [id] => 10
                            [pid] => 9
                            [name] => 曹杨路
                        )
                )
        )
    [10] => Array
        (
            [id] => 10
            [pid] => 9
            [name] => 曹杨路
        )
    [1] => Array
        (
            [id] => 1
            [pid] => 0
            [name] => 北京市
            [son] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [pid] => 1
                            [name] => 东城区
                        )
                    [1] => Array
                        (
                            [id] => 4
                            [pid] => 1
                            [name] => 朝阳区
                        )
                )
        )
    [2] => Array
        (
            [id] => 2
            [pid] => 0
            [name] => 上海市
            [son] => Array
                (
                    [0] => Array
                        (
                            [id] => 6
                            [pid] => 2
                            [name] => 徐汇区
                            [son] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 7
                                            [pid] => 6
                                            [name] => 徐家汇
                                        )
                                )
                        )
                    [1] => Array
                        (
                            [id] => 8
                            [pid] => 2
                            [name] => 黄浦区
                        )
                    [2] => Array
                        (
                            [id] => 9
                            [pid] => 2
                            [name] => 普陀区
                            [son] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 10
                                            [pid] => 9
                                            [name] => 曹杨路
                                        )
                                )
                        )
                )
        )
    [3] => Array
        (
            [id] => 3
            [pid] => 1
            [name] => 东城区
        )
    [4] => Array
        (
            [id] => 4
            [pid] => 1
            [name] => 朝阳区
        )
    [5] => Array
        (
            [id] => 5
            [pid] => 1
            [name] => 海淀区
        )
)

三.

$tree[]=&$items[$k]
同样利用引用取出自己需要的数据完成分类
猜你喜欢