> 백엔드 개발 > PHP 튜토리얼 > PHP无限极分门别类

PHP无限极分门别类

WBOY
풀어 주다: 2016-06-13 12:18:48
원래의
942명이 탐색했습니다.

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">'&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>
로그인 후 복사

果然是厉害,代码简洁精炼,无需递归,执行速度快。这是我偶然在一个网站上看到的,觉得很实用就收藏下来,分享给大家。
网站地址为: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 无限极分类

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿