php生成无限栏目树的方法
程序员文章站
2022-03-30 08:56:58
...
本篇文章主要介绍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); ?>
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
thinkPHP实现递归循环栏目并按照树形结构无限极输出的方法
以上就是php生成无限栏目树的方法的详细内容,更多请关注其它相关文章!