首页 > 代码库 > 递归参考

递归参考

function get_cat($cid=0){//递归算法    
        $nav=M(‘nav‘);
        $cat=$nav->where(‘pre_id=‘.$cid)->select();
        foreach($cat as $key=>$val){
            $cats=$this->get_cat($val[‘id‘]);
            $cat[$key][‘child‘]=$cats;
        }
        return $cat;
    }

 

Array
(
    [0] => Array
        (
            [id] => 4
            [nav_name] => 研报
            [pre_id] => 0
            [pid] => 4
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [nav_name] => 宏观
                            [pre_id] => 4
                            [pid] => 17
                            [child] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 6
                            [nav_name] => 行业
                            [pre_id] => 4
                            [pid] => 18
                            [child] => Array
                                (
                                )

                        )

                    [2] => Array
                        (
                            [id] => 7
                            [nav_name] => 个股
                            [pre_id] => 4
                            [pid] => 19
                            [child] => Array
                                (
                                )

                        )

                    [3] => Array
                        (
                            [id] => 8
                            [nav_name] => 名家
                            [pre_id] => 4
                            [pid] => 20
                            [child] => Array
                                (
                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 2
            [nav_name] => 市场
            [pre_id] => 0
            [pid] => 2
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 9
                            [nav_name] => 大盘
                            [pre_id] => 2
                            [pid] => 9
                            [child] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 10
                            [nav_name] => 主力
                            [pre_id] => 2
                            [pid] => 10
                            [child] => Array
                                (
                                )

                        )

                    [2] => Array
                        (
                            [id] => 11
                            [nav_name] => 基金
                            [pre_id] => 2
                            [pid] => 11
                            [child] => Array
                                (
                                )

                        )

                    [3] => Array
                        (
                            [id] => 12
                            [nav_name] => 策略
                            [pre_id] => 2
                            [pid] => 12
                            [child] => Array
                                (
                                )

                        )

                )

        )

    [2] => Array
        (
            [id] => 3
            [nav_name] => 理财
            [pre_id] => 0
            [pid] => 3
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 13
                            [nav_name] => 银行
                            [pre_id] => 3
                            [pid] => 13
                            [child] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 14
                            [nav_name] => 债券
                            [pre_id] => 3
                            [pid] => 14
                            [child] => Array
                                (
                                )

                        )

                    [2] => Array
                        (
                            [id] => 15
                            [nav_name] => 保险
                            [pre_id] => 3
                            [pid] => 15
                            [child] => Array
                                (
                                )

                        )

                    [3] => Array
                        (
                            [id] => 16
                            [nav_name] => 信托
                            [pre_id] => 3
                            [pid] => 16
                            [child] => Array
                                (
                                )

                        )

                )

        )

    [3] => Array
        (
            [id] => 1
            [nav_name] => 全球
            [pre_id] => 0
            [pid] => 1
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 17
                            [nav_name] => 美股
                            [pre_id] => 1
                            [pid] => 5
                            [child] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 18
                            [nav_name] => 外汇
                            [pre_id] => 1
                            [pid] => 6
                            [child] => Array
                                (
                                )

                        )

                    [2] => Array
                        (
                            [id] => 19
                            [nav_name] => 期货
                            [pre_id] => 1
                            [pid] => 7
                            [child] => Array
                                (
                                )

                        )

                    [3] => Array
                        (
                            [id] => 20
                            [nav_name] => 黄金
                            [pre_id] => 1
                            [pid] => 8
                            [child] => Array
                                (
                                )

                        )

                )

        )

)

递归参考