Heim > php教程 > php手册 > 生成树型结构(比如省市区)

生成树型结构(比如省市区)

WBOY
Freigeben: 2016-05-25 18:02:51
Original
1135 Leute haben es durchsucht
<br><span style="color: #000000;">php
</span><span style="color: #008080;">header</span>("Content-type: text/html; charset=utf-8"<span style="color: #000000;">);

</span><span style="color: #0000ff;">function</span> generateTree(<span style="color: #800080;">$items</span><span style="color: #000000;">)
{
    </span><span style="color: #800080;">$tree</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
    </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$items</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$item</span><span style="color: #000000;">){
        </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$items</span>[<span style="color: #800080;">$item</span>['pid'<span style="color: #000000;">]])){
            </span><span style="color: #800080;">$items</span>[<span style="color: #800080;">$item</span>['pid']]['son'][] = &<span style="color: #800080;">$items</span>[<span style="color: #800080;">$item</span>['id'<span style="color: #000000;">]];
        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{
            </span><span style="color: #800080;">$tree</span>[] = &<span style="color: #800080;">$items</span>[<span style="color: #800080;">$item</span>['id'<span style="color: #000000;">]];
        }
    }
    </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$tree</span><span style="color: #000000;">;
}<br>//需要注意的一点,数组的key值必须与id值保持一致
</span><span style="color: #800080;">$items</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(
    </span>1 => <span style="color: #0000ff;">array</span>('id' => 1, 'pid' => 0, 'name' => '安徽省'),
    2 => <span style="color: #0000ff;">array</span>('id' => 2, 'pid' => 0, 'name' => '浙江省'),
    3 => <span style="color: #0000ff;">array</span>('id' => 3, 'pid' => 1, 'name' => '合肥市'),
    4 => <span style="color: #0000ff;">array</span>('id' => 4, 'pid' => 3, 'name' => '长丰县'),
    5 => <span style="color: #0000ff;">array</span>('id' => 5, 'pid' => 1, 'name' => '安庆市'),<span style="color: #000000;">
);
</span><span style="color: #0000ff;">echo</span> "<pre class="brush:php;toolbar:false">"<span style="color: #000000;">;
</span><span style="color: #008080;">print_r</span>(generateTree(<span style="color: #800080;">$items</span>));
Nach dem Login kopieren

 

上面方法的优化:

<span style="color: #000000;">php
</span><span style="color: #008080;">header</span>("Content-type: text/html; charset=utf-8"<span style="color: #000000;">);

</span><span style="color: #0000ff;">function</span> generateTree(<span style="color: #800080;">$items</span><span style="color: #000000;">)
{
    </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$items</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$item</span><span style="color: #000000;">)
        </span><span style="color: #800080;">$items</span>[<span style="color: #800080;">$item</span>['pid']]['son'][<span style="color: #800080;">$item</span>['id']] = &<span style="color: #800080;">$items</span>[<span style="color: #800080;">$item</span>['id'<span style="color: #000000;">]];
    </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$items</span>[0]['son']) ? <span style="color: #800080;">$items</span>[0]['son'] : <span style="color: #0000ff;">array</span><span style="color: #000000;">();
}

</span><span style="color: #008000;">//</span><span style="color: #008000;">需要注意的一点,数组的key值必须与id保持一致</span>
<span style="color: #800080;">$items</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(
    </span>1 => <span style="color: #0000ff;">array</span>('id' => 1, 'pid' => 0, 'name' => '安徽省'),
    2 => <span style="color: #0000ff;">array</span>('id' => 2, 'pid' => 0, 'name' => '浙江省'),
    3 => <span style="color: #0000ff;">array</span>('id' => 3, 'pid' => 1, 'name' => '合肥市'),
    4 => <span style="color: #0000ff;">array</span>('id' => 4, 'pid' => 3, 'name' => '长丰县'),
    5 => <span style="color: #0000ff;">array</span>('id' => 5, 'pid' => 1, 'name' => '安庆市'),<span style="color: #000000;">
);
</span><span style="color: #0000ff;">echo</span> "<pre class="brush:php;toolbar:false">"<span style="color: #000000;">;
</span><span style="color: #008080;">print_r</span>(generateTree(<span style="color: #800080;">$items</span>));
Nach dem Login kopieren
View Code

 

结果:

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage