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

无限级分类列表

WBOY
WBOY원래의
2016-05-25 18:02:511177검색
<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>);

运行结果:

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.