Home > Backend Development > PHP Tutorial > php无限级分门别类

php无限级分门别类

WBOY
Release: 2016-06-13 12:54:23
Original
1000 people have browsed it

php无限级分类

CI的seven给我提供了一个方法,如下:

<?php /**
 * 此方法由@Tonton 提供
 * http://my.oschina.net/u/918697
 * @date 2012-12-12 
 */
function genTree5($items) { 
    foreach ($items as $item) 
        $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']]; 
    return isset($items[0]['son']) ? $items[0]['son'] : array(); 
} 

/**
 * 将数据格式化成树形结构
 * @author Xuefen.Tong
 * @param array $items
 * @return array 
 */
function genTree9($items) {
    $tree = array(); //格式化好的树
    foreach ($items as $item)
        if (isset($items[$item['pid']]))
            $items[$item['pid']]['son'][] = &$items[$item['id']];
        else
            $tree[] = &$items[$item['id']];
    return $tree;
}

$items = array(
    1 => array('id' => 1, 'pid' => 0, 'name' => '江西省'),
    2 => array('id' => 2, 'pid' => 0, 'name' => '黑龙江省'),
    3 => array('id' => 3, 'pid' => 1, 'name' => '南昌市'),
    4 => array('id' => 4, 'pid' => 2, 'name' => '哈尔滨市'),
    5 => array('id' => 5, 'pid' => 2, 'name' => '鸡西市'),
    6 => array('id' => 6, 'pid' => 4, 'name' => '香坊区'),
    7 => array('id' => 7, 'pid' => 4, 'name' => '南岗区'),
    8 => array('id' => 8, 'pid' => 6, 'name' => '和兴路'),
    9 => array('id' => 9, 'pid' => 7, 'name' => '西大直街'),
    10 => array('id' => 10, 'pid' => 8, 'name' => '东北林业大学'),
    11 => array('id' => 11, 'pid' => 9, 'name' => '哈尔滨工业大学'),
    12 => array('id' => 12, 'pid' => 8, 'name' => '哈尔滨师范大学'),
    13 => array('id' => 13, 'pid' => 1, 'name' => '赣州市'),
    14 => array('id' => 14, 'pid' => 13, 'name' => '赣县'),
    15 => array('id' => 15, 'pid' => 13, 'name' => '于都县'),
    16 => array('id' => 16, 'pid' => 14, 'name' => '茅店镇'),
    17 => array('id' => 17, 'pid' => 14, 'name' => '大田乡'),
    18 => array('id' => 18, 'pid' => 16, 'name' => '义源村'),
    19 => array('id' => 19, 'pid' => 16, 'name' => '上坝村'),
);
echo "<pre class="brush:php;toolbar:false">";
print_r(genTree5($items));
print_r(genTree9($items));

//后者输出格式,前者类似,只是数组键值不一样,不过不影响数据结构
/*
Array
(
[0] => Array
    (
        [id] => 1
        [pid] => 0
        [name] => 江西省
        [son] => Array
            (
                [0] => Array
                    (
                        [id] => 3
                        [pid] => 1
                        [name] => 南昌市
                    )

                [1] => Array
                    (
                        [id] => 13
                        [pid] => 1
                        [name] => 赣州市
                        [son] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 14
                                        [pid] => 13
                                        [name] => 赣县
                                        [son] => Array
                                            (
                                            [0] => Array
                                                (
                                                    [id] => 16
                                                    [pid] => 14
                                                    [name] => 茅店镇
                                                    [son] => Array
                                                        (
                                                        [0] => Array
                                                            (
                                                            [id] => 18
                                                            [pid] => 16
                                                            [name] => 义源村
                                                            )

                                                        [1] => Array
                                                            (
                                                            [id] => 19
                                                            [pid] => 16
                                                            [name] => 上坝村
                                                            )

                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [id] => 17
                                                    [pid] => 14
                                                    [name] => 大田乡
                                                )

                                            )

                                    )

                                [1] => Array
                                    (
                                        [id] => 15
                                        [pid] => 13
                                        [name] => 于都县
                                    )

                            )

                    )

            )

    )

[1] => Array
    (
        [id] => 2
        [pid] => 0
        [name] => 黑龙江省
        [son] => Array
            (
                [0] => Array
                    (
                        [id] => 4
                        [pid] => 2
                        [name] => 哈尔滨市
                        [son] => Array
                            (
                            [0] => Array
                                (
                                    [id] => 6
                                    [pid] => 4
                                    [name] => 香坊区
                                    [son] => Array
                                        (
                                        [0] => Array
                                            (
                                                [id] => 8
                                                [pid] => 6
                                                [name] => 和兴路
                                                [son] => Array
                                                    (
                                                        [0] => Array
                                                            (
                                                            [id] => 10
                                                            [pid] => 8
                                                            [name] => 
                                                             东北林业大学
                                                            )

                                                        [1] => Array
                                                            (
                                                            [id] => 12
                                                            [pid] => 8
                                                            [name] => 
                                                            哈尔滨师范大学
                                                            )

                                                    )

                                            )

                                        )

                                )

                            [1] => Array
                                (
                                    [id] => 7
                                    [pid] => 4
                                    [name] => 南岗区
                                    [son] => Array
                                        (
                                        [0] => Array
                                            (
                                            [id] => 9
                                            [pid] => 7
                                            [name] => 西大直街
                                            [son] => Array
                                                (
                                                [0] => Array
                                                    (
                                                    [id] => 11
                                                    [pid] => 9
                                                    [name] => 
                                                     哈尔滨工业大学
                                                    )

                                                )

                                            )

                                        )

                                )

                            )

                    )

                [1] => Array
                    (
                        [id] => 5
                        [pid] => 2
                        [name] => 鸡西市
                    )

            )

    )
)*
Copy after login


通过测试,能够完成所设想的分类无限级,但是,用他自有数据可以,但是用我数据库的数据不可以,会出现错误,比如会出现如下错误:

Array
(
    [10] => Array
        (
            [child] => Array
                (
                    [14] => 
                )

        )

    [11] => 
    [12] => 
    [9] => Array
        (
            [child] => Array
                (
                    [13] => 
                )

        )

)
Copy after login

只显示数组序号,不显示相应数据,这是我很苦恼,这样测试后能够完全显示:


function genTree5($items) { 
    foreach ($items as $item) {
	$items[$item['id']]=$item;//ADD THIS
        $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']]; 
		}
    return isset($items[0]['son']) ? $items[0]['son'] : array(); 
} 
Copy after login

但是这只在id升序状态下可以使用,乱序会显示错误


虽然在大多情况下id会保持升序状态,但是如果涉及到排序后,遍无法使用了。

之前,CI的吕老师提供了一种高效方法,即:


id 父id   分级   
1   0      0,1
2   1     0,1,2
3   1     0,1,3
4   3    0,1,3,4

select * from 表  order  by 分级 asc
Copy after login

这个结构,会使分级按照父子顺序排序,首先对第一个0排序,然后对第二个1排序,以此类推,自动分类,十分好用(同时注意,一定要保持有两组,也就是0,1开始或者0,0开始,否则根分级会提前显示)。“分级”也可以叫childid,因为是记录的子id层。

|--2
|----2-1
|------2-1-1
|--3
|----3-1
|--4
|--1
|----1-1
Copy after login
显示直接遍历即可,因为我有一个depth字段,记录深度,所以能更好的显示。

这个方法运行时间也较短,微秒级运行,总之耗费在数据库排序上了,其他的基本很愉快。

利用指针的一般在没有缓存情况下测试大多数大于排序后的时间,大多数是CPU时间,而且,结果不准确。


PS:PHP已经对地址,指针之类的概念弱化了,也就是说,并不是PHP的特点,虽然可以使用,但是会出现一些莫名错误。所以还是老老实实的吧~神马大数据算法,就不用PHP来做了,C++什么的,干这个,比PHP速度快多了。。。。

同时 @Tonton 对他的算法进行修正。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template