PHP递归方法实现无限分类的另一种实现
以前我写过一次关于PHP无限分类的实现方法,其实很简单,就是实现对数组的排序,并且加上lv(层次)属性,实现无限分类的方法有很多,你可以直接在数据结构上进行设计,当然递归的方法是比较常用的,不过,不适合大型数据结构,如果你想看以前我写的那个无限分类的方法请点击:
http://www.chhua.com/web-note967,下面是另外一种方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<span style="color: #000000;font-weight: bold">class</span> fenlei<span style="color: #009900">{</span>
<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$fanganArr</span><span style="color: #339933">;</span><span style="color: #666666;font-style: italic">//待排序数组</span>
<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000088">$treeArr</span><span style="color: #339933">;</span>	<span style="color: #666666;font-style: italic">//排序完成之后的数组</span>
 
<span style="color: #000000;font-weight: bold">private</span> <span style="color: #000000;font-weight: bold">function</span> getfanganArr<span style="color: #009900">(</span><span style="color: #000088">$pid</span><span style="color: #339933">=</span><span style="color: #cc66cc">0</span><span style="color: #339933">,</span><span style="color: #000088">$lv</span><span style="color: #339933">=</span><span style="color: #cc66cc">0</span><span style="color: #009900">)</span><span style="color: #009900">{</span><span style="color: #666666;font-style: italic">//递归创建无限分类结构</span>
       <span style="color: #000088">$lv</span><span style="color: #339933">++;</span><span style="color: #666666;font-style: italic">//lv是层次属性</span>
       <span style="color: #b1b100">foreach</span> <span style="color: #009900">(</span><span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">fanganArr</span> <span style="color: #b1b100">as</span> <span style="color: #000088">$sort</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
       	<span style="color: #b1b100">if</span> <span style="color: #009900">(</span><span style="color: #000088">$sort</span><span style="color: #009900">[</span><span style="color: #0000ff">"fangan_fuid"</span><span style="color: #009900">]</span><span style="color: #339933">==</span><span style="color: #000088">$pid</span><span style="color: #009900">)</span><span style="color: #009900">{</span>
       		<span style="color: #000088">$sort</span><span style="color: #009900">[</span><span style="color: #0000ff">"lv"</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #000088">$lv</span><span style="color: #339933">;</span>
       		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">treeArr</span><span style="color: #009900">[</span><span style="color: #009900">]</span><span style="color: #339933">=</span><span style="color: #000088">$sort</span><span style="color: #339933">;</span>
       		<span style="color: #000088">$this</span><span style="color: #339933">-></span><span style="color: #004000">getfanganArr</span><span style="color: #009900">(</span><span style="color: #000088">$sort</span><span style="color: #009900">[</span><span style="color: #0000ff">"fangan_id"</span><span style="color: #009900">]</span><span style="color: #339933">,</span><span style="color: #000088">$lv</span><span style="color: #009900">)</span><span style="color: #339933">;</span>
       	<span style="color: #009900">}</span>
       <span style="color: #009900">}</span>
	<span style="color: #009900">}</span>
<span style="color: #009900">}</span>