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

无限级分类列表

WBOY
Libérer: 2016-05-25 18:02:51
original
1151 Les gens l'ont consulté
<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>);
Copier après la connexion

运行结果:

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal