递归到第N级

时间:2017-05-23 10:37:42

标签: php arrays laravel multidimensional-array laravel-5

这是我的单一数据库结构。

enter image description here

我有一个数组,我将通过var_export在一个文件中导出。

这是样本数组,

return [
    'menus'    => [
        'products' => [
            'name' => 'Products',
            'link' => 'cctv.html',
            'show' => true,
        ],
        'companies'     => [
            'name' => 'Companies',
            'link' => 'companies.html',
            'show' => true,
        ],
    ],
    'products' => [
        'section_enabled' => [
            'featured_products', 'categories', 'news', 'latest_products', 'case_studies', 'expert_commentary',
            'videos', 'companies', 'topics',
        ],
        'pages'           => [
            'child_category_page'  => [
                'middle' => [
                    'left' => [
                        'class'    => 'col-md-9',
                        'sections' => [
                            [
                                'folder'    => '',
                                'element'   => 'page_heading',
                                'variables' => ['page_heading', 'articles'],
                            ],
                        ],
                    ],
                ],
            ],
            'product_profile_page' => [
                'middle' => [
                    'left' => [
                        'class'    => 'col-md-9',
                        'sections' => [
                            [
                                'folder'    => '',
                                'element'   => 'page_heading',
                                'variables' => ['page_heading'],
                            ],
                            [
                                'folder'    => 'products',
                                'element'   => 'specifications',
                                'variables' => ['specifications', 'filters'],
                            ],
                        ],
                    ],
                ],
            ],
        ],

    ],
    'news'     => [
        'news_types'        => [
            'expert_commentary' => 'Etary',
            'applications'      => 'Mon',
            'security_beat'     => 'SB',
            'round_table'       => 'RT',
            'case_studies'      => [
                'type'  => 'Marcation',
                'label' => 'Casies',
            ],
        ],
        'url_lookup_values' => [
            'caudies' => [
                'value'  => 'Marklication',
                'config' => 'castudies',
            ],
        ],
    ],
]; 

我从here获得了递归技巧。

但它并没有解决我的问题。

这是我尝试过的,

function generate_site_config()
{
    $data = DB::table("SITE_CONFIGS")->where("parent_id", 0)->get();
    $desired_array = [];
    get_tree_site_configs($data,$desired_array,0,0);

    // file_put_contents(config_path() . DIRECTORY_SEPARATOR . "frontend_configs_demo.php", '<?php return ' . var_export($data, true) . ';');
    echo "success";die;
}
function get_tree_site_configs($inputArray, &$outputArray, $parent = 0, $level = 0){

    foreach($inputArray as $cc_id => $sub_arr){

        if($sub_arr->parent_id == $parent){
            $outputArray[] = $sub_arr;
            if($sub_arr->variable_value == ''){
                $inputArray = DB::table("SITE_CONFIGS")->where("parent_id", $sub_arr->id)->get();
                get_tree_site_configs($inputArray, $outputArray, $sub_arr->id, $level + 1);
            }else{
                pr($sub_arr);
                pr($outputArray);die;
            }
        } 
    }

}
  

注意:链将一直运行到variable_value ==&#39;&#39;,如果找到variable_value然后它会在那一端停止树,那么它将寻找其他正在悬空的父母。

0 个答案:

没有答案