PHP Infinitus 分類

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

PHP Infinitus 分類

概要

??実際の業務では Infinitus 分類がよく使われます。ナビテーブルなど。 PHP Infinitus の分類をネットで調べてみると、たくさんありましたが、同じものが多く、しかも、非常に書き方が汚く、信頼性の低いコードが多かったので、Infinitus をいじる必要がありました。自分なりの分類。以下に 2 つの無限分類を紹介します。递归使用引用

データの準備

<code class="language-php hljs "><span class="hljs-comment">//初始化原始数据(id=>编号,name=>显示名称,pid=>父级目录id,sort=>排序顺序)</span><span class="hljs-variable">$data</span>[<span class="hljs-number">1</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'1'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'一级目录A'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'0'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'1'</span>);<span class="hljs-variable">$data</span>[<span class="hljs-number">2</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'2'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'一级目录B'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'0'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'2'</span>);<span class="hljs-variable">$data</span>[<span class="hljs-number">3</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'3'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'一级目录C'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'0'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'3'</span>);<span class="hljs-variable">$data</span>[<span class="hljs-number">4</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'4'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'一级目录D'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'0'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'4'</span>);<span class="hljs-variable">$data</span>[<span class="hljs-number">5</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'5'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'二级目录A-1'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'1'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'1'</span>);<span class="hljs-variable">$data</span>[<span class="hljs-number">6</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'6'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'二级目录A-2'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'1'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'2'</span>);<span class="hljs-variable">$data</span>[<span class="hljs-number">7</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'7'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'二级目录A-3'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'1'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'3'</span>);<span class="hljs-variable">$data</span>[<span class="hljs-number">8</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'8'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'二级目录B-1'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'2'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'1'</span>);<span class="hljs-variable">$data</span>[<span class="hljs-number">9</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'9'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'二级目录B-2'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'2'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'2'</span>);<span class="hljs-variable">$data</span>[<span class="hljs-number">10</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'10'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'二级目录B-3'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'2'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'3'</span>);<span class="hljs-variable">$data</span>[<span class="hljs-number">11</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'11'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'二级目录C-1'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'3'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'2'</span>);<span class="hljs-variable">$data</span>[<span class="hljs-number">12</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'12'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'二级目录D-1'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'4'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'1'</span>);<span class="hljs-variable">$data</span>[<span class="hljs-number">13</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'13'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'二级目录D-2'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'4'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'2'</span>);<span class="hljs-variable">$data</span>[<span class="hljs-number">14</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'14'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'三级目录A-2-1'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'6'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'1'</span>);<span class="hljs-variable">$data</span>[<span class="hljs-number">15</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'15'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'三级目录A-2-2'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'6'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'2'</span>);<span class="hljs-variable">$data</span>[<span class="hljs-number">16</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'16'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'三级目录C-1-1'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'11'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'1'</span>);<span class="hljs-variable">$data</span>[<span class="hljs-number">17</span>] = <span class="hljs-keyword">array</span>(<span class="hljs-string">'id'</span>=><span class="hljs-string">'17'</span>,<span class="hljs-string">'name'</span>=><span class="hljs-string">'三级目录B-2-1'</span>,<span class="hljs-string">'pid'</span>=><span class="hljs-string">'9'</span>,<span class="hljs-string">'sort'</span>=><span class="hljs-string">'2'</span>);</code>

最初にツリー構造を表示する関数を作成します

<code class="language-php hljs "><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">printTree</span><span class="hljs-params">(<span class="hljs-variable">$data</span>,<span class="hljs-variable">$level</span>=<span class="hljs-number">0</span>)</span>{</span>    <span class="hljs-keyword">foreach</span>(<span class="hljs-variable">$data</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$key</span>=><span class="hljs-variable">$value</span>){        <span class="hljs-keyword">for</span>(<span class="hljs-variable">$i</span>=<span class="hljs-number">0</span>;<span class="hljs-variable">$i</span><=<span class="hljs-variable">$level</span>;<span class="hljs-variable">$i</span>++){            <span class="hljs-keyword">echo</span> <span class="hljs-string">'&emsp;&emsp;'</span>;        }        <span class="hljs-keyword">echo</span> <span class="hljs-variable">$value</span>[<span class="hljs-string">'name'</span>];        <span class="hljs-keyword">echo</span> <span class="hljs-string">'<br>'</span>;        <span class="hljs-keyword">if</span>(!<span class="hljs-keyword">empty</span>(<span class="hljs-variable">$value</span>[<span class="hljs-string">'children'</span>])){            printTree(<span class="hljs-variable">$value</span>[<span class="hljs-string">'children'</span>],<span class="hljs-variable">$level</span>+<span class="hljs-number">1</span>);        }    }}</code>

再帰無限分類

再帰を使用して無限分類配列を取得します

<code class="language-php hljs "><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getNodeTree</span><span class="hljs-params">(&<span class="hljs-variable">$list</span>,&<span class="hljs-variable">$tree</span>,<span class="hljs-variable">$pid</span>=<span class="hljs-number">0</span>)</span>{</span>    <span class="hljs-keyword">foreach</span>(<span class="hljs-variable">$list</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">$pid</span> == <span class="hljs-variable">$value</span>[<span class="hljs-string">'pid'</span>]){            <span class="hljs-variable">$tree</span>[<span class="hljs-variable">$value</span>[<span class="hljs-string">'id'</span>]]=<span class="hljs-variable">$value</span>;            <span class="hljs-keyword">unset</span>(<span class="hljs-variable">$list</span>[<span class="hljs-variable">$key</span>]);            getNodeTree(<span class="hljs-variable">$list</span>,<span class="hljs-variable">$tree</span>[<span class="hljs-variable">$value</span>[<span class="hljs-string">'id'</span>]][<span class="hljs-string">'children'</span>],<span class="hljs-variable">$value</span>[<span class="hljs-string">'id'</span>]);        }    }}</code>

参照無限分類を使用します

<code class="language-php hljs "><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">createNodeTree</span><span class="hljs-params">(&<span class="hljs-variable">$list</span>,&<span class="hljs-variable">$tree</span>)</span>{</span>    <span class="hljs-keyword">foreach</span>(<span class="hljs-variable">$list</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$key</span>=><span class="hljs-variable">$node</span>){        <span class="hljs-keyword">if</span>(<span class="hljs-keyword">isset</span>(<span class="hljs-variable">$list</span>[<span class="hljs-variable">$node</span>[<span class="hljs-string">'pid'</span>]])){            <span class="hljs-variable">$list</span>[<span class="hljs-variable">$node</span>[<span class="hljs-string">'pid'</span>]][<span class="hljs-string">'children'</span>][] = &<span class="hljs-variable">$list</span>[<span class="hljs-variable">$key</span>];        }<span class="hljs-keyword">else</span>{            <span class="hljs-variable">$tree</span>[] = &<span class="hljs-variable">$list</span>[<span class="hljs-variable">$node</span>[<span class="hljs-string">'id'</span>]];        }    }}</code>

本当に素晴らしいです。コードは簡潔で簡潔で、再帰は必要なく、実行速度も速いです。たまたまウェブサイトで見かけてとても便利だったので保存してみんなにシェアしました。
Web サイトのアドレスは次のとおりです: http://www.phpddt.com/php/generateTree.html

呼び出し

<code class="language-php hljs "><span class="hljs-comment">//递归-无限极分类调用</span>getNodeTree(<span class="hljs-variable">$data</span>,<span class="hljs-variable">$tree</span>);printTree(<span class="hljs-variable">$tree</span>);<span class="hljs-comment">//使用引用-无限极分类调用</span>createNodeTree(<span class="hljs-variable">$data</span>,<span class="hljs-variable">$tree</span>);printTree(<span class="hljs-variable">$tree</span>);</code>

結果

php 无限极分类

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