1

Here is input array with an n-level depth

array
(
    [0] => array
    (
        [id]             => 1
        [parent_id]      => 0
        [variable_name]  => menus
        [variable_value] =>
        [children]       => array
        (
            [0] => array
            (
                [id]             => 2
                [parent_id]      => 1
                [variable_name]  => products
                [variable_value] => {"name":"Products", "link":"cctv.html", "show":"1"},
            )
            [1] => array
            (
                [id]             => 3
                [parent_id]      => 1
                [variable_name]  => companies
                [variable_value] => {"name":"Companies", "link":"companies.html", "show":"1"},
            ),
        ),
    )
    [1] => array
    (
        [id]             => 4
        [parent_id]      => 0
        [variable_name]  => breadcrumbs
        [variable_value] =>
        [children]       => array
        (
            [0] => array
            (
                [id]        => 5
                [parent_id] => 4,
            )
            [1] => array
            (
                [id]             => 6
                [parent_id]      => 4
                [variable_name]  => companies
                [variable_value] => {"name":"Companies", "link":"companies.html", "show":"1"},
            ),
        ),
    )
    [2] => array
    (
        [id]             => 7
        [parent_id]      => 0
        [variable_name]  => products
        [variable_value] =>
        [children]       => array
        (
            [0] => array
            (
                [id]             => 8
                [parent_id]      => 7
                [variable_name]  => pages
                [variable_value] =>
                [children]       => array
                (
                    [0] => array
                    (
                        [id]             => 9
                        [parent_id]      => 8
                        [variable_name]  => child_category_page
                        [variable_value] =>
                        [children]       => array
                        (
                            [0] => array
                            (
                                [id]             => 10
                                [parent_id]      => 9
                                [variable_name]  => middle
                                [variable_value] =>
                                [children]       => array
                                (
                                    [0] => array
                                    (
                                        [id]             => 11
                                        [parent_id]      => 10
                                        [variable_name]  => left
                                        [variable_value] => {"name":"Companies", "link":"companies.html", "show":"1", "test":1},
                                    ),
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
)

I want to convert it into,

[
    'menus'       => [
        'products'  => [
            'name' => 'Products',
            'link' => 'cctv.html',
            'show' => true,
        ],
        'companies' => [
            'name' => 'Companies',
            'link' => 'companies.html',
            'show' => true,
        ],

    ],
    'breadcrumbs' => [
        'news'      => [
            'text' => 'News',
            'show' => true,
            'link' => 'news.html',
        ],
        'companies' => [
            'text' => 'Companies',
            'show' => true,
            'link' => 'companies.html',
        ],

    ],
    'products'    => [
        'pages' => [
            'child_category_page' => [
                'middle' => [
                    'left' => [
                        'text' => 'Companies',
                        'show' => true,
                        'link' => 'companies.html',
                    ],
                ],
            ],

        ],
    ],

];

What I have tried is,

$data = DB::table("SITE_CONFIGS")->where("parent_id", 0)->get();
$data = get_tree_site_configs($data);

function get_tree_site_configs($data, $parent=0, &$result=[]){
    $data = json_decode(json_encode($data),true);

    $branch = [];

    foreach ($data as $key => &$value) {
        if($parent == $value['parent_id']){
            $has_sub = DB::table("SITE_CONFIGS")->where("parent_id", $value['id'])->get();
            $children = get_tree_site_configs($has_sub, $value['id'],$result);

            if($children){
                $value['children'] = $children;
            }
            // pr($value);
            $branch[] = $value;
        }
    }

    return $branch;
}

Note: There is parent-child relation to n-level, parent id with variable_value are leaf notes, means they don't have any children, rest all are parents of some records.

1 Answer 1

3
function get_tree_site_configs(array $config) {
    $result = [];
    foreach ($config as $item) {
        if (isset($item['children'])) {
            $value = get_tree_site_configs($item['children']);
        } else {
            $value = $item['variable_value'];
        }

        $result[$item['variable_name']] = $value;
    }

    return $result;
}
Sign up to request clarification or add additional context in comments.

2 Comments

ohh, wait its working, its done !!!!! :D Thanks A TONSSSSSNNN. I don't have words how to thank you mate.. Thank you vvvvvvvery much !!!!!
Sure mate, (y) :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.