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 递归函数 实现无限极递归分类列表

 

上一篇:C#比较实体与实体或集合实体与集合实体的数据比较


下一篇:php遍历数组初学小结