首页 > 代码库 > PHP无限极分类

PHP无限极分类

1、数据库设计。

技术分享

 

2、PHP代码。

//一维数组的形式

技术分享
function tree(&$list, $pid = 0, $level = 0, $html = ‘----------‘) {
static $tree = array();
foreach ($list as $v) {
if ($v[‘pid‘] == $pid) {
$v[‘level‘] = $level;
$v[‘html‘] = str_repeat($html, $level);
$tree[] = $v;
tree($list, $v[‘id‘], $level + 1);
}
}
return $tree;
}
View Code

 

//多维数组的形式

技术分享
function toLayer($cate, $pid = 0){
$arr = array();
foreach ($cate as $v){
if ($v[‘pid‘] == $pid){
$v[‘child‘] = toLayer($cate, $v[‘id‘]);
$arr[] = $v;
}
}
return $arr;
}
View Code

 

PHP无限极分类