建立数据表
CREATE TABLE IF NOT EXISTS `category` (
`categoryId` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`parentId` smallint(5) unsigned NOT NULL DEFAULT '0',
`categoryName` varchar(50) NOT NULL,
PRIMARY KEY (`categoryId`)
) ;
INSERT INTO `category` (`categoryId`, `parentId`, `categoryName`) VALUES
(1, 0, 'php'),
(2, 0, 'java'),
(3, 0, 'c/c++'),
(4, 1, 'php基础'),
(5, 1, 'php开源资料'),
(6, 1, 'php框架'),
(7, 2, 'java Se'),
(8, 2, 'java EE'),
(9, 2, 'java Me'),
(10, 3, 'c/c++基础编程'),
(11, 3, 'c/c++系统开发'),
(12, 3, 'c嵌入式编程'),
(13, 3, 'c++应用开发'),
(14, 13, 'c++桌面应用开发'),
(15, 13, 'c++游戏开发');
下面是php代码:
<?php
//获取某分类的直接子分类
function getSons($categorys,$catId=0){
$sons=array();
foreach($categorys as $item){
if($item['parentId']==$catId)
$sons[]=$item;
}
return $sons;
}
//获取某个分类的所有子分类
function getSubs($categorys,$catId=0,$level=1){
$subs=array();
foreach($categorys as $item){
if($item['parentId']==$catId){
$item['level']=$level;
$subs[]=$item;
$subs=array_merge($subs,getSubs($categorys,$item['categoryId'],$level+1));
}
}
return $subs;
}
//获取某个分类的所有父分类
//方法一,递归
function getParents($categorys,$catId){
$tree=array();
foreach($categorys as $item){
if($item['categoryId']==$catId){
if($item['parentId']>0)
$tree=array_merge($tree,getParents($categorys,$item['parentId']));
$tree[]=$item;
break;
}
}
return $tree;
}
//方法二,迭代
function getParents2($categorys,$catId){
$tree=array();
while($catId != 0){
foreach($categorys as $item){
if($item['categoryId']==$catId){
$tree[]=$item;
$catId=$item['parentId'];
break;
}
}
}
return $tree;
}
//测试 部分
$pdo=new PDO('mysql:host=localhost;dbname=test','root','8888');
$stmt=$pdo->query("select * from category order by categoryId");
$categorys=$stmt->fetchAll(PDO::FETCH_ASSOC);
$result=getSons($categorys,1);
foreach($result as $item)
echo $item['categoryName'].'<br>';
echo '<hr>';
$result=getSubs($categorys,0);
foreach($result as $item)
echo str_repeat(' ',$item['level']).$item['categoryName'].'<br>';
echo '<hr>';
$result=getParents($categorys,7);
foreach($result as $item)
echo $item['categoryName'].' >> ';
echo '<hr>';
$result=getParents2($categorys,15);
foreach($result as $item)
echo $item['categoryName'].' >> ';
?>
下面是运行的结果:
//获取所有子分类
$category_list = array(
array('id'=>1, 'name'=>'1级分类 1', 'pid'=>0),
array('id'=>2, 'name'=>'1级分类 2', 'pid'=>0),
array('id'=>3, 'name'=>'1级分类 3', 'pid'=>0),
array('id'=>4, 'name'=>'2级分类 1', 'pid'=>1),
array('id'=>5, 'name'=>'2级分类 2', 'pid'=>2),
array('id'=>6, 'name'=>'3级分类 1', 'pid'=>4),
array('id'=>7, 'name'=>'3级分类 2', 'pid'=>5),
array('id'=>8, 'name'=>'4级分类 1', 'pid'=>6),
array('id'=>9, 'name'=>'3级分类 2', 'pid'=>5),
);
$son_list = getSubs($category_list, 2);
function getSubs($categorys,$catId=0,$level=1){
$subs=array();
foreach($categorys as $item){
if($item['pid']==$catId){
$item['level']=$level;
$subs[]=$item;
$subs=array_merge($subs,getSubs($categorys,$item['id'],$level+1));
}
}
print_r($subs);
return $subs;
}
例子://获取所有子分类
$category_list = array(
array('id'=>1, 'name'=>'1级分类 1', 'pid'=>0),
array('id'=>2, 'name'=>'1级分类 2', 'pid'=>0),
array('id'=>3, 'name'=>'1级分类 3', 'pid'=>0),
array('id'=>4, 'name'=>'2级分类 1', 'pid'=>1),
array('id'=>5, 'name'=>'2级分类 2', 'pid'=>2),
array('id'=>6, 'name'=>'3级分类 1', 'pid'=>4),
array('id'=>7, 'name'=>'3级分类 2', 'pid'=>5),
array('id'=>8, 'name'=>'4级分类 1', 'pid'=>6),
array('id'=>9, 'name'=>'3级分类 2', 'pid'=>5),
);
$a = getSonx(1, $category_list);
//print_r($a);exit;
function getSonx($id, $data){
$returnData = array();
foreach($data as $k=>$v){
if($v['pid'] == $id){
$returnData[] = $v;
$a = getSonx($v['id'], $data);
if($a){
print_r($a);
$returnData = array_merge($returnData, $a);
}
//从最底层数据,向上合并。
//for pid=5层,id:7,9是最底层了,getSonx()返回的是2个空array因为(没有子类)
//结果:$returnData = array(7,9); 即$returnData[];
//for pid=2层,id:5 合并刚才的7,9 $returnData = array(7,9);
//结果:$returnData = array(5,7,9);
}
}
return $returnData;
}
exit;
//递归获取所有的子分类的ID,最终返回一个数组如: [3,5,7]
function get_all_child($array,$id){
$arr = array();
foreach($array as $v){
if($v['pid'] == $id){
$arr[] = $v['cat_id'];
$arr = array_merge($arr,$this->get_all_child($array,$v['cat_id']));
};
};
return $arr;
}