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

Php 如何从递归数组创建递归数组?,php,arrays,laravel,recursion,multidimensional-array,Php,Arrays,Laravel,Recursion,Multidimensional Array,这是具有n级深度的输入数组 array ( [0] => array ( [id] => 1 [parent_id] => 0 [variable_name] => menus [variable_value] => [children] => array ( [0] =>

这是具有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级有父子关系,带变量_值的父id为叶注释,表示它们没有任何子项,其余都是某些记录的父项


哦,等等,它工作了,它完成了D谢谢你。我不知道该如何感谢你,伙计。。非常感谢你!!!!!当然,伙计,(y):D
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;
}