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

无限级分类列表

WBOY
WBOYOriginal
2016-05-25 18:02:511154browse
<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>);

运行结果:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn