PHP无限极分类
??在实际工作中,经常要用到无限极分类。如导航表等等。到网上一搜php无限极分类,很多,但好多都是一个,并且,写的很乱,代码很多,那些都不靠谱,还是自己捣鼓捣鼓无限极分类了。下面介绍两种无限极分类:递归
,使用引用
<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">'  '</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>
果然是厉害,代码简洁精炼,无需递归,执行速度快。这是我偶然在一个网站上看到的,觉得很实用就收藏下来,分享给大家。
网站地址为: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>