PHP 递归读取无限级分类

时间:2024-08-10 23:33:32
 /**
* 【获取第一级分类】
* 20170829
*
* @return array
*/
public function getCateList(){
$this->catelog = [];
//获取第一级分类
$rst = $this->find()->where(['is_root'=>0, 'status' => 10])->asArray()->all(); if($rst){
foreach ($rst as $row){
$this->catelog[] = [
'id' => $row['id_cate'],
'catename' => $row['catename'],
'catename_en' => $row['catename_en'],
'cover' => $row['cover'],
'depath' => 0,
'is_root' => $row['is_root'],
];
//获取子分类
$this->getChild($row['id_cate'], 1); }
} return $this->catelog;
} /**
* 【递归获取所有子分类】
* 20170829
*
* @param $id
* @param $depth
*/
function getChild($id,$depth ){
//global $catalog; $rst = $this->find()->where(['is_root' => $id, 'status' => 10])->asArray()->all();
if (count($rst) > 0) {
foreach ($rst as $row){
$this->catelog[] = [
'id' => $row['id_cate'],
'catename' => $row['catename'],
'catename_en' => $row['catename_en'],
'cover' => $row['cover'],
'depath' => $depth,
'is_root' => $row['is_root'],
];
$this->getChild($row['id_cate'], 2);
}
} }