php 栏目树,利用php生成无限栏目树方法介绍

这篇文章讲述利用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 '

';

print_r( $tree);

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值