ホームページ php教程 PHP源码 php实现无限级分类的树形结构

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

May 23, 2016 pm 05:10 PM

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] => 鸡西市
                    )
 
            )
 
    )
)*/
ログイン後にコピー
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)