无限分类数据库查询采用递归方法组成有层级数组

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

数据库结构,没有记录层级level

无限分类数据库查询采用递归方法组成有层级数组_第1张图片

public function get_child_tree_v2($id = 0,$level=0)
{

    $three_arr = array();
    $map['parent_id'] = $id;
    $res = Db::table("ecs_category")
            ->where($map)
            ->field('cat_id,cat_name,parent_id,is_show,cat_ico,sort_order')
            ->order('sort_order,cat_id')
            ->select();

    if($res){
        $level++;
        foreach ($res as $row) {
            $three_arr [$row ['cat_id']] ['cat_id'] = $row ['cat_id'];
            $three_arr[$row ['cat_id']] ['cat_name'] = $row ['cat_name'];
            $three_arr[$row ['cat_id']] ['level'] = $level;
            $three_arr[$row ['cat_id']]['is_show'] = $this->return_is_show($row ['is_show']);
            $three_arr[$row ['cat_id']]['sort_order'] = $row ['sort_order'];
            $three_arr [$row ['cat_id']] ['child'] = $this->get_child_tree_v2($row ['cat_id'], $level);
        }
    }


    return $three_arr;
}

output

  [132] => Array
        (
            [cat_id] => 132
            [cat_name] => 速冻食品
            [level] => 1
            [is_show] => 是
            [sort_order] => 50
            [child] => Array
                (
                    [133] => Array
                        (
                            [cat_id] => 133
                            [cat_name] => 冰冻海鲜
                            [level] => 2
                            [is_show] => 否
                            [sort_order] => 50
                            [child] => Array
                                (
                                    [134] => Array
                                        (
                                            [cat_id] => 134
                                            [cat_name] => 冰冻肉类
                                            [level] => 3
                                            [is_show] => 否
                                            [sort_order] => 50
                                            [child] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [135] => Array
                        (
                            [cat_id] => 135
                            [cat_name] => 冰冻包装食品
                            [level] => 2
                            [is_show] => 否
                            [sort_order] => 50
                            [child] => Array
                                (
                                )

                        )

                    [136] => Array
                        (
                            [cat_id] => 136
                            [cat_name] => 火腿类
                            [level] => 2
                            [is_show] => 否
                            [sort_order] => 50
                            [child] => Array
                                (
                                )

                        )

                )

        )

 

转载于:https://my.oschina.net/xuyaoxiang/blog/775565

你可能感兴趣的:(无限分类数据库查询采用递归方法组成有层级数组)