• php生成无限栏目树


    栏目数组:
    $arr=Array(

        Array('cid' => 2,'cname' => '新闻','pid' => 0),
        Array('cid' => 4,'cname' =>'体育','pid' => 0),
        Array('cid' => 5,'cname' => '娱乐','pid' => 0),
        Array('cid' => 7,'cname' => '热点新闻','pid' =>2),
        Array('cid' => 8,'cname' => '小众新闻','pid' => 2),
        Array('cid' => 9,'cname' => '民谣新闻','pid' => 8),

    );

    <?php
    
    $arr=Array(
        Array('cid' => 2,'cname' => '新闻','pid' => 0),
        Array('cid' => 4,'cname' =>'体育','pid' => 0),
        Array('cid' => 5,'cname' => '娱乐','pid' => 0),
        Array('cid' => 7,'cname' => '热点新闻','pid' =>2),
        Array('cid' => 8,'cname' => '小众新闻','pid' => 2),
        Array('cid' => 9,'cname' => '民谣新闻','pid' => 8),
    
    );
    function formatTree($array, $pid = 0,$level = 1,$field = array('pid'=>'pid','id'=>'id','children'=>'children')){
            $arr = array();
            foreach ($array as &$v) {
                if ($v[$field['pid']] == $pid) {
                    $v['level'] = $level;
                    $tem = formatTree($array, $v[$field['id']],$v['level']+1,$field);
                    //判断是否存在子数组
                    $tem && $v[$field['children']] = $tem;
                    $arr[] = $v;
                }
            }
            return $arr;
        }
    
    $tree = formatTree( $arr,0,1, $field = array('pid'=>'pid','id'=>'cid','children'=>'children') );
    echo '<pre>';
    print_r( $tree);
    
    ?>
    

      

  • 相关阅读:
    Chaos网络库(一) 开篇介绍
    对Oracle中的表进行分区
    java socket编程
    SQL优化
    冒泡排序、选择排序和插入
    集合有趣的地方
    java中String s = new String("abc")创建了几个对象
    集合排序
    接口与抽象类的区别
    软件测试流程
  • 原文地址:https://www.cnblogs.com/ceet/p/6552303.html
Copyright © 2020-2023  润新知