如何从递归数组创建递归数组?

时间:2017-05-24 12:01:48

标签: php arrays laravel recursion multidimensional-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是叶子注释,意味着它们没有任何子级,其余都是某些记录的父级。

1 个答案:

答案 0 :(得分: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;
}