标签:非递归 func php无限分类 gen log sse generate bsp lse
//这个是核心方法
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));
标签:非递归 func php无限分类 gen log sse generate bsp lse
原文地址:https://www.cnblogs.com/Ares0023/p/9395450.html