簡體   English   中英

如何從遞歸數組創建遞歸數組?

[英]How to Create recursive array from recursive array?

這是具有n級深度的輸入數組

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},
                                    ),
                                ),
                            ),
                        ),
                    ),
                ),
            ),
        ),
    ),
)

我想把它轉換成,

[
    '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',
                    ],
                ],
            ],

        ],
    ],

];

我試過的是,

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

注意:有n級的父子關系,帶有variable_value的父級id是葉子注釋,意味着它們沒有任何子級,其余都是某些記錄的父級。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM