Maison > php教程 > PHP源码 > le corps du texte

php实现无限级分类的树形结构

PHP中文网
Libérer: 2016-05-23 17:10:02
original
1036 Les gens l'ont consulté

tree.php

<?php
 
/**
 * 此方法由@Tonton 提供
 * http://my.oschina.net/u/918697
 * @date 2012-12-12 
 */
function genTree5($items) { 
    foreach ($items as $item) 
        $items[$item[&#39;pid&#39;]][&#39;son&#39;][$item[&#39;id&#39;]] = &$items[$item[&#39;id&#39;]]; 
    return isset($items[0][&#39;son&#39;]) ? $items[0][&#39;son&#39;] : array(); 
} 
 
/**
 * 将数据格式化成树形结构
 * @author Xuefen.Tong
 * @param array $items
 * @return array 
 */
function genTree9($items) {
    $tree = array(); //格式化好的树
    foreach ($items as $item)
        if (isset($items[$item[&#39;pid&#39;]]))
            $items[$item[&#39;pid&#39;]][&#39;son&#39;][] = &$items[$item[&#39;id&#39;]];
        else
            $tree[] = &$items[$item[&#39;id&#39;]];
    return $tree;
}
 
$items = array(
    1 => array(&#39;id&#39; => 1, &#39;pid&#39; => 0, &#39;name&#39; => &#39;江西省&#39;),
    2 => array(&#39;id&#39; => 2, &#39;pid&#39; => 0, &#39;name&#39; => &#39;黑龙江省&#39;),
    3 => array(&#39;id&#39; => 3, &#39;pid&#39; => 1, &#39;name&#39; => &#39;南昌市&#39;),
    4 => array(&#39;id&#39; => 4, &#39;pid&#39; => 2, &#39;name&#39; => &#39;哈尔滨市&#39;),
    5 => array(&#39;id&#39; => 5, &#39;pid&#39; => 2, &#39;name&#39; => &#39;鸡西市&#39;),
    6 => array(&#39;id&#39; => 6, &#39;pid&#39; => 4, &#39;name&#39; => &#39;香坊区&#39;),
    7 => array(&#39;id&#39; => 7, &#39;pid&#39; => 4, &#39;name&#39; => &#39;南岗区&#39;),
    8 => array(&#39;id&#39; => 8, &#39;pid&#39; => 6, &#39;name&#39; => &#39;和兴路&#39;),
    9 => array(&#39;id&#39; => 9, &#39;pid&#39; => 7, &#39;name&#39; => &#39;西大直街&#39;),
    10 => array(&#39;id&#39; => 10, &#39;pid&#39; => 8, &#39;name&#39; => &#39;东北林业大学&#39;),
    11 => array(&#39;id&#39; => 11, &#39;pid&#39; => 9, &#39;name&#39; => &#39;哈尔滨工业大学&#39;),
    12 => array(&#39;id&#39; => 12, &#39;pid&#39; => 8, &#39;name&#39; => &#39;哈尔滨师范大学&#39;),
    13 => array(&#39;id&#39; => 13, &#39;pid&#39; => 1, &#39;name&#39; => &#39;赣州市&#39;),
    14 => array(&#39;id&#39; => 14, &#39;pid&#39; => 13, &#39;name&#39; => &#39;赣县&#39;),
    15 => array(&#39;id&#39; => 15, &#39;pid&#39; => 13, &#39;name&#39; => &#39;于都县&#39;),
    16 => array(&#39;id&#39; => 16, &#39;pid&#39; => 14, &#39;name&#39; => &#39;茅店镇&#39;),
    17 => array(&#39;id&#39; => 17, &#39;pid&#39; => 14, &#39;name&#39; => &#39;大田乡&#39;),
    18 => array(&#39;id&#39; => 18, &#39;pid&#39; => 16, &#39;name&#39; => &#39;义源村&#39;),
    19 => array(&#39;id&#39; => 19, &#39;pid&#39; => 16, &#39;name&#39; => &#39;上坝村&#39;),
);
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] => 鸡西市
                    )
 
            )
 
    )
)*/
Copier après la connexion
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal