Heim >php教程 >php手册 >无限级分类列表

无限级分类列表

WBOY
WBOYOriginal
2016-05-25 18:02:511151Durchsuche
<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: #800080;">$arr</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(
    </span>0=><span style="color: #0000ff;">array</span>('Id'=>1,'Name'=>'文件管理','Pid'=>0),
    1=><span style="color: #0000ff;">array</span>('Id'=>2,'Name'=>'来源管理','Pid'=>0),
    2=><span style="color: #0000ff;">array</span>('Id'=>3,'Name'=>'文件添加','Pid'=>1),
    3=><span style="color: #0000ff;">array</span>('Id'=>4,'Name'=>'文件删除','Pid'=>1),
    4=><span style="color: #0000ff;">array</span>('Id'=>5,'Name'=>'人员列表','Pid'=>2),
    5=><span style="color: #0000ff;">array</span>('Id'=>6,'Name'=>'添加用户','Pid'=>5),
    6=><span style="color: #0000ff;">array</span>('Id'=>7,'Name'=>'编辑用户','Pid'=>5),<span style="color: #000000;">
);

</span><span style="color: #0000ff;">function</span> getTree(<span style="color: #800080;">$arr</span>, <span style="color: #800080;">$pid</span>=0,<span style="color: #800080;">$deep</span>=0<span style="color: #000000;">)
{
    </span><span style="color: #0000ff;">static</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;">$arr</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$k</span>=><span style="color: #800080;">$row</span><span style="color: #000000;">)
    {
        </span><span style="color: #0000ff;">if</span>( <span style="color: #800080;">$row</span>['Pid'] == <span style="color: #800080;">$pid</span><span style="color: #000000;"> )
        {
            </span><span style="color: #800080;">$row</span>['deep'] = <span style="color: #800080;">$deep</span><span style="color: #000000;">;
            </span><span style="color: #800080;">$tree</span>[] = <span style="color: #800080;">$row</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$arr</span>[<span style="color: #800080;">$k</span><span style="color: #000000;">]);
            getTree(</span><span style="color: #800080;">$arr</span>,<span style="color: #800080;">$row</span>['Id'],<span style="color: #800080;">$deep</span>+1<span style="color: #000000;">);
        }
    }
    </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$tree</span><span style="color: #000000;">;
}

</span><span style="color: #800080;">$res</span> = getTree(<span style="color: #800080;">$arr</span><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>(<span style="color: #800080;">$res</span>);

运行结果:

Stellungnahme:
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