递归找分类下的所有子类

      if ($_POST['category_id']) {
                $idArray = Array();
                $categoryList = $this->getSubCategory($_POST['category_id'],$m_product_category->select(),''); 
                    var_dump($categoryList);die;
                foreach ($categoryList as $value) {
                    $idArray[] = $value['category_id'];
                }
                $idList = empty($idArray) ? $_POST['category_id'] : $_POST['category_id'].','.implode(',', $idArray);
                $where['category_id'] = array('IN',$idList);         
        }


// $category_id 分类id      $category  查询表中所有 二维数组 
function getSubCategory($category_id, $category, $separate) {
    $array = array(); 
    foreach($category AS $value) {
        if ($category_id == $value['parent_id']) {
            $array[] = array('category_id' => $value['category_id'], 'name' => $separate.$value['name'],'description'=>$value['description']);
            $array = array_merge($array, getSubCategory($value['category_id'], $category, $separate.'--'));
        }
    }
    return $array;
}
posted @ 2019-12-19 14:45  凡斗  阅读(413)  评论(0编辑  收藏  举报