欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php生成无限栏目树_php技巧

程序员文章站 2022-04-21 20:14:28
...
本文主要介绍了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); 
?> 

以下主要介绍了PHP超牛逼无限极分类生成树方法,巧用PHP中的引用实现树的生成方法,比递归方法高端多了,需要的朋友可以参考下

function generateTree($items){
    $tree = array();
    foreach($items as $item){
        if(isset($items[$item['pid']])){
            $items[$item['pid']]['son'][] = &$items[$item['id']];
        }else{
            $tree[] = &$items[$item['id']];
        }
    }
    return $tree;
}
$items = array(
    1 => array('id' => 1, 'pid' => 0, 'name' => '安徽省'),
    2 => array('id' => 2, 'pid' => 0, 'name' => '浙江省'),
    3 => array('id' => 3, 'pid' => 1, 'name' => '合肥市'),
    4 => array('id' => 4, 'pid' => 3, 'name' => '长丰县'),
    5 => array('id' => 5, 'pid' => 1, 'name' => '安庆市'),
);
print_r(generateTree($items));

可以看到下面打印的结果:

Array
(
    [0] => Array
        (
            [id] => 1
            [pid] => 0
            [name] => 安徽省
            [son] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [pid] => 1
                            [name] => 合肥市
                            [son] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 4
                                            [pid] => 3
                                            [name] => 长丰县
                                        )
 
                                )
 
                        )
 
                    [1] => Array
                        (
                            [id] => 5
                            [pid] => 1
                            [name] => 安庆市
                        )
 
                )
 
        )
 
    [1] => Array
        (
            [id] => 2
            [pid] => 0
            [name] => 浙江省
        )
 
)

上面生成树方法还可以精简到5行:

function generateTree($items){
    foreach($items as $item)
        $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']];
    return isset($items[0]['son']) ? $items[0]['son'] : array();
}

上面这种无限极分类数据树形结构化的方法值得借鉴。但是我觉得这段代码实际用途并不明显啊,你想取出格式化的树形数据还是要递归啊:

/**
 * 如何取数据格式化的树形数据
 */
$tree = generateTree($items);
function getTreeData($tree){
    foreach($tree as $t){
        echo $t['name'].'<br>';
        if(isset($t['son'])){
            getTreeData($t['son']);
        }
    }
}
getTreeData($tree);

以上就是php生成无限栏目树_php技巧的详细内容,更多请关注其它相关文章!

相关标签: php 技巧 栏目