ホームページ  >  記事  >  バックエンド開発  >  PHP は Infinitus 分類を実装します

PHP は Infinitus 分類を実装します

WBOY
WBOYオリジナル
2016-06-13 12:23:081000ブラウズ

PHP は無限分類を実装します

2 つの for ループを使用して無限分類を実装します

テーブル:

字段名 字段类型 备注 默认值
id int 主键 auto-increment
name varchar 分类名称
pid int 父类id 0

トップカテゴリーのデフォルトの pid は 0 です。特定のカテゴリのサブカテゴリ ツリーを取り出したい場合、基本的な考え方は 再帰 です。 もちろん、効率の問題のため データベース にクエリを実行することはお勧めできません。通常のアプローチは、最初にすべてのカテゴリを取り出し、データを PHP 配列に保存し、次に を処理して、次のリクエストの効率を向上させます。

最初に

元の配列を構築します。これはデータベースから直接クエリできます:

1. データを構築します。 🎜>

目標は、次の構造に変換することです
<code class=" hljs php"><span class="hljs-variable">$categories</span> = <span class="hljs-keyword">array</span>(    <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-number">1</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'电脑'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-number">0</span>),    <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-number">2</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'手机'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-number">0</span>),    <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-number">3</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'笔记本'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-number">1</span>),    <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-number">4</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'台式机'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-number">1</span>),    <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-number">5</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'智能机'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-number">2</span>),    <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-number">6</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'功能机'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-number">2</span>),    <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-number">7</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'超级本'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-number">3</span>),    <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-number">8</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'游戏本'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-number">3</span>),);</code>
コンピューター>ノートブック>
ウルトラブック>デスクトップ

スマートフォン>

配列で表される場合、

そのサブカテゴリを保存する子キーを追加できます
:

2. 処理プロセス:
<code class=" hljs php"><span class="hljs-keyword">array</span>(    <span class="hljs-comment">//1对应$categories中的id ,方便直接读取</span>    <span class="hljs-number">1</span> => <span class="hljs-keyword">array</span>(        <span class="hljs-string">'id'</span>=><span class="hljs-number">1</span>,        <span class="hljs-string">'name'</span>=><span class="hljs-string">'电脑'</span>,        <span class="hljs-string">'pid'</span>=><span class="hljs-number">0</span>,        children=><span class="hljs-keyword">array</span>(            &<span class="hljs-keyword">array</span>(                <span class="hljs-string">'id'</span>=><span class="hljs-number">3</span>,                <span class="hljs-string">'name'</span>=><span class="hljs-string">'笔记本'</span>,                <span class="hljs-string">'pid'</span>=><span class="hljs-number">1</span>,                <span class="hljs-string">'children'</span>=><span class="hljs-keyword">array</span>(                    <span class="hljs-comment">//此处省略</span>                )            ),            &<span class="hljs-keyword">array</span>(                <span class="hljs-string">'id'</span>=><span class="hljs-number">4</span>,                <span class="hljs-string">'name'</span>=><span class="hljs-string">'台式机'</span>,                <span class="hljs-string">'pid'</span>=><span class="hljs-number">1</span>,                <span class="hljs-string">'children'</span>=><span class="hljs-keyword">array</span>(                    <span class="hljs-comment">//此处省略</span>                )            ),        )    ),    <span class="hljs-comment">//其他分类省略</span>)</code>

<code class=" hljs php"><span class="hljs-variable">$tree</span> = <span class="hljs-keyword">array</span>();<span class="hljs-comment">//第一步,将所有的分类id作为数组key,并创建children单元</span><span class="hljs-keyword">foreach</span>(<span class="hljs-variable">$categories</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$category</span>){    <span class="hljs-variable">$tree</span>[<span class="hljs-variable">$category</span>[<span class="hljs-string">'id'</span>]] = <span class="hljs-variable">$category</span>;    <span class="hljs-variable">$tree</span>[<span class="hljs-variable">$category</span>[<span class="hljs-string">'id'</span>]][<span class="hljs-string">'children'</span>] = <span class="hljs-keyword">array</span>();}<span class="hljs-comment">//第二步,利用引用,将每个分类添加到父类children数组中,这样一次遍历即可形成树形结构。</span><span class="hljs-keyword">foreach</span> (<span class="hljs-variable">$tree</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$key</span>=><span class="hljs-variable">$value</span>) {    <span class="hljs-keyword">if</span> (<span class="hljs-variable">$value</span>[<span class="hljs-string">'pid'</span>] != <span class="hljs-number">0</span>) {        <span class="hljs-variable">$tree</span>[<span class="hljs-variable">$value</span>[<span class="hljs-string">'pid'</span>]][<span class="hljs-string">'children'</span>][] = &<span class="hljs-variable">$tree</span>[<span class="hljs-variable">$key</span>];    }}print_r(<span class="hljs-variable">$tree</span>);</code>
3. 印刷結果は次のとおりです。
<code>注:必须通过引用, 否则不会一次遍历就生成最终的结果.</code>

<code class=" hljs php"><span class="hljs-keyword">Array</span>(    [<span class="hljs-number">1</span>] => <span class="hljs-keyword">Array</span>        (            [id] => <span class="hljs-number">1</span>            [name] => 电脑            [pid] => <span class="hljs-number">0</span>            [children] => <span class="hljs-keyword">Array</span>                (                    [<span class="hljs-number">0</span>] => <span class="hljs-keyword">Array</span>                        (                            [id] => <span class="hljs-number">3</span>                            [name] => 笔记本                            [pid] => <span class="hljs-number">1</span>                            [children] => <span class="hljs-keyword">Array</span>                                (                                    [<span class="hljs-number">0</span>] => <span class="hljs-keyword">Array</span>                                        (                                            [id] => <span class="hljs-number">7</span>                                            [name] => 超级本                                            [pid] => <span class="hljs-number">3</span>                                            [children] => <span class="hljs-keyword">Array</span>                                                (                                                )                                        )                                    [<span class="hljs-number">1</span>] => <span class="hljs-keyword">Array</span>                                        (                                            [id] => <span class="hljs-number">8</span>                                            [name] => 游戏本                                            [pid] => <span class="hljs-number">3</span>                                            [children] => <span class="hljs-keyword">Array</span>                                                (                                                )                                        )                                )                        )                    [<span class="hljs-number">1</span>] => <span class="hljs-keyword">Array</span>                        (                            [id] => <span class="hljs-number">4</span>                            [name] => 台式机                            [pid] => <span class="hljs-number">1</span>                            [children] => <span class="hljs-keyword">Array</span>                                (                                )                        )                )        )    [<span class="hljs-number">2</span>] => <span class="hljs-keyword">Array</span>        (            [id] => <span class="hljs-number">2</span>            [name] => 手机            [pid] => <span class="hljs-number">0</span>            [children] => <span class="hljs-keyword">Array</span>                (                    [<span class="hljs-number">0</span>] => <span class="hljs-keyword">Array</span>                        (                            [id] => <span class="hljs-number">5</span>                            [name] => 智能机                            [pid] => <span class="hljs-number">2</span>                            [children] => <span class="hljs-keyword">Array</span>                                (                                )                        )                    [<span class="hljs-number">1</span>] => <span class="hljs-keyword">Array</span>                        (                            [id] => <span class="hljs-number">6</span>                            [name] => 功能机                            [pid] => <span class="hljs-number">2</span>                            [children] => <span class="hljs-keyword">Array</span>                                (                                )                        )                )        )    [<span class="hljs-number">3</span>] => <span class="hljs-keyword">Array</span>        (            [id] => <span class="hljs-number">3</span>            [name] => 笔记本            [pid] => <span class="hljs-number">1</span>            [children] => <span class="hljs-keyword">Array</span>                (                    [<span class="hljs-number">0</span>] => <span class="hljs-keyword">Array</span>                        (                            [id] => <span class="hljs-number">7</span>                            [name] => 超级本                            [pid] => <span class="hljs-number">3</span>                            [children] => <span class="hljs-keyword">Array</span>                                (                                )                        )                    [<span class="hljs-number">1</span>] => <span class="hljs-keyword">Array</span>                        (                            [id] => <span class="hljs-number">8</span>                            [name] => 游戏本                            [pid] => <span class="hljs-number">3</span>                            [children] => <span class="hljs-keyword">Array</span>                                (                                )                        )                )        )    [<span class="hljs-number">4</span>] => <span class="hljs-keyword">Array</span>        (            [id] => <span class="hljs-number">4</span>            [name] => 台式机            [pid] => <span class="hljs-number">1</span>            [children] => <span class="hljs-keyword">Array</span>                (                )        )    [<span class="hljs-number">5</span>] => <span class="hljs-keyword">Array</span>        (            [id] => <span class="hljs-number">5</span>            [name] => 智能机            [pid] => <span class="hljs-number">2</span>            [children] => <span class="hljs-keyword">Array</span>                (                )        )    [<span class="hljs-number">6</span>] => <span class="hljs-keyword">Array</span>        (            [id] => <span class="hljs-number">6</span>            [name] => 功能机            [pid] => <span class="hljs-number">2</span>            [children] => <span class="hljs-keyword">Array</span>                (                )        )    [<span class="hljs-number">7</span>] => <span class="hljs-keyword">Array</span>        (            [id] => <span class="hljs-number">7</span>            [name] => 超级本            [pid] => <span class="hljs-number">3</span>            [children] => <span class="hljs-keyword">Array</span>                (                )        )    [<span class="hljs-number">8</span>] => <span class="hljs-keyword">Array</span>        (            [id] => <span class="hljs-number">8</span>            [name] => 游戏本            [pid] => <span class="hljs-number">3</span>            [children] => <span class="hljs-keyword">Array</span>                (                )        ))</code>
利点:
の関係は次のとおりです。明確であり、上司と部下の関係を修正するのは簡単です。

デメリット:

PHP 処理を使用すると、カテゴリの数が膨大になると効率も低下します。

原文 http://blog.csdn.net/u013927110/article/details/44086253

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。