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

无限级分类列表,级分类列表

WBOY
Lepaskan: 2016-06-13 08:39:11
asal
768 orang telah melayarinya

无限级分类列表,级分类列表

<?<span>php
</span><span>header</span>("Content-type: text/html; charset=utf-8"<span>);
</span><span>$arr</span> = <span>array</span><span>(
    </span>0=><span>array</span>('Id'=>1,'Name'=>'文件管理','Pid'=>0),
    1=><span>array</span>('Id'=>2,'Name'=>'来源管理','Pid'=>0),
    2=><span>array</span>('Id'=>3,'Name'=>'文件添加','Pid'=>1),
    3=><span>array</span>('Id'=>4,'Name'=>'文件删除','Pid'=>1),
    4=><span>array</span>('Id'=>5,'Name'=>'人员列表','Pid'=>2),
    5=><span>array</span>('Id'=>6,'Name'=>'添加用户','Pid'=>5),
    6=><span>array</span>('Id'=>7,'Name'=>'编辑用户','Pid'=>5),<span>
);

</span><span>function</span> getTree(<span>$arr</span>, <span>$pid</span>=0,<span>$deep</span>=0<span>)
{
    </span><span>static</span> <span>$tree</span> = <span>array</span><span>();
    </span><span>foreach</span>(<span>$arr</span> <span>as</span> <span>$k</span>=><span>$row</span><span>)
    {
        </span><span>if</span>( <span>$row</span>['Pid'] == <span>$pid</span><span> )
        {
            </span><span>$row</span>['deep'] = <span>$deep</span><span>;
            </span><span>$tree</span>[] = <span>$row</span><span>;
            </span><span>unset</span>(<span>$arr</span>[<span>$k</span><span>]);
            getTree(</span><span>$arr</span>,<span>$row</span>['Id'],<span>$deep</span>+1<span>);
        }
    }
    </span><span>return</span> <span>$tree</span><span>;
}

</span><span>$res</span> = getTree(<span>$arr</span><span>);
</span><span>echo</span> "<pre class="brush:php;toolbar:false">"<span>;
</span><span>print_r</span>(<span>$res</span>);
Salin selepas log masuk

运行结果:

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan