Tuesday, May 23, 2017

Recursion as per condition to n level

I have one array which I will export in one file by var_export.

This is sample array,

return [
    'menus'    => [
        'products' => [
            'name' => 'Products',
            'link' => 'products.html',
            'show' => true,
        ],
        'news'     => [
            'name' => 'News',
            'link' => 'news.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',
            ],
        ],
    ],
]; 

I got the recursion trick from here.

But it doesn't solve my problem.

Here is what I have tried,

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;
            }
        } 
    }

}

NOTE : chain will go until variable_value == '', if variable_value found then it stops the tree at that end, then it will look for other parents which are dangling.



via rahul_m

Advertisement