php添加分类的方法,PHP实现无限级分类的方法

PHP实现无限级分类的方法

9

// get_tree_option()返回数组,并为每个元素增加了“深度”(即depth)列,直接输出即可

$options = get_tree_option($list, 1);

foreach($options as $op) {

echo '' . str_repeat(" ", $op['depth'] * 4) . $op['title'] . '';

}

?>

5. 查找某一分类的所有子类

4

$children = get_tree_child($list, 0);

echo implode(',', $children); // 输出:1,3,2,7,6,5,8,4

?>

6. 查找某一分类的所有父类

$children = get_tree_parent($list, 4);

echo implode(',', $children); //8, 2, 10

?>

7. 相关函数

180

function get_tree_child($data, $fid) {

$result = array();

$fids = array($fid);

do {

$cids = array();

$flag = false;

foreach($fids as $fid) {

for($i = count($data) - 1; $i >=0 ; $i--) {

$node = $data[$i];

if($node['fid'] == $fid) {

array_splice($data, $i , 1);

$result[] = $node['id'];

$cids[] = $node['id'];

$flag = true;

}

}

}

$fids = $cids;

} while($flag === true);

return $result;

}

function get_tree_parent($data, $id) {

$result = array();

$obj = array();

foreach($data as $node) {

$obj[$node['id']] = $node;

}

$value = isset($obj[$id]) ? $obj[$id] : null;

while($value) {

$id = null;

foreach($data as $node) {

if($node['id'] == $value['fid']) {

$id = $node['id'];

$result[] = $node['id'];

break;

}

}

if($id === null) {

$result[] = $value['fid'];

}

$value = isset($obj[$id]) ? $obj[$id] : null;

}

unset($obj);

return $result;

}

function get_tree_ul($data, $fid) {

$stack = array($fid);

$child = array();

$added_left = array();

$added_right= array();

$html_left = array();

$html_right = array();

$obj = array();

$loop = 0;

foreach($data as $node) {

$pid = $node['fid'];

if(!isset($child[$pid])) {

$child[$pid] = array();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值