PHP教程

php 递归函数 实现无限极递归分类列表

本文主要是介绍php 递归函数 实现无限极递归分类列表,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.方法

//递归函数 实现无限级分类列表
    function get_cate_list($list,$pid=0,$level=0) {
        static $tree = array();
        foreach($list as $row) {
            if($row['pid']==$pid) {
                $row['level'] = $level;
                $tree[] = $row;
                get_cate_list($list, $row['id'], $level + 1);
            }
        }
        return $tree;
    }

 

在前端如何展示出来

<div class="menu_dropdown bk_2">
        {foreach $nav as $k => $nav_one}
        {foreach $nav_one['son'] as $kk=>$nav_two}
        <dl id="menu{$kk}" class="sec_nav" data-pid="{$nav_one.id}" style="{if($nav_one.id == $current_auth_ids[1])}display:block;{else/}display:none;{/if}">
            <dt class="{if($nav_two.id == $current_auth_ids[2])}selected{/if}"><i class="Hui-iconfont">&#xe616;</i> {$nav_two.auth_name}<i class="Hui-iconfont menu_dropdown-arrow">&#xe6d5;</i></dt>
            <dd style="{if($nav_two.id == $current_auth_ids[2])}display:block;{/if}">
                <ul>
                    {foreach $nav_two['son'] as $nav_three}
                    <li data-id="{$nav_three.id}" class="{if($nav_three.id == $current_auth_ids[3])}current{/if}">
                        <a href="{if($nav_three['auth_c']&&$nav_three['auth_a'])}{:url($nav_three['auth_c'] . '/' . $nav_three['auth_a'])}{else/}javascript:;{/if}" title="">{$nav_three.auth_name}</a>
                    </li>
                    {/foreach}
                </ul>
            </dd>
        </dl>
        {/foreach}
        {/foreach}
    </div>

效果图如下

 

这篇关于php 递归函数 实现无限极递归分类列表的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!