PHP-(几乎)递归展平多维数组

PHP-(几乎)递归展平多维数组,php,arrays,recursion,multidimensional-array,Php,Arrays,Recursion,Multidimensional Array,我正在尝试将以下数组更改为几乎平坦的数组。因此,ID4将位于数组的第一级,ID6和ID5也一样,但仍然有自己的索引,这样我就可以知道哪个页面是哪个页面。但顺序和现在一样。我假设解决方案是某种递归PHP函数,但我不知道如何做到这一点 Array ( [0] => Array ( [id] => 2 [identifier] => External URL [parent] => 0 [sortOrder

我正在尝试将以下数组更改为几乎平坦的数组。因此,ID4将位于数组的第一级,ID6和ID5也一样,但仍然有自己的索引,这样我就可以知道哪个页面是哪个页面。但顺序和现在一样。我假设解决方案是某种递归PHP函数,但我不知道如何做到这一点

Array
(
[0] => Array
    (
        [id] => 2
        [identifier] => External URL
        [parent] => 0
        [sortOrder] => 1
        [depth] => 0
    )

[1] => Array
    (
        [id] => 3
        [identifier] => First Team
        [parent] => 0
        [sortOrder] => 2
        [depth] => 0
        [children] => Array
            (
                [0] => Array
                    (
                        [id] => 4
                        [identifier] => League tables
                        [parent] => 3
                        [sortOrder] => 0
                        [depth] => 1
                        [children] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 6
                                        [identifier] => British and Irish Cup Tables
                                        [parent] => 4
                                        [sortOrder] => 24
                                        [depth] => 2
                                    )

                                [1] => Array
                                    (
                                        [id] => 5
                                        [identifier] => Greene King IPA Championship
                                        [parent] => 4
                                        [sortOrder] => 25
                                        [depth] => 2
                                    )

                            )

                    )

            )

    )

[2] => Array
    (
        [id] => 1
        [identifier] => Home
        [parent] => 0
        [sortOrder] => 25
        [depth] => 0
    )

)

我找到了解决办法!我构建了一个递归PHP函数,该函数使用深度索引跟踪每个项目的级别,同时保持数组平坦(ish)


不幸的是,此解决方案可能重复,无法解决我的问题。我以前尝试过这段代码,它只显示数组中的第一个索引。您尝试过所有答案吗?您可以发布预期的输出是什么以及您迄今为止尝试过的内容吗?这对我们会有很大帮助的
<?php

$data = [
    [
        'id' => 1,
        'name' => 'one',
        'children' =>
        [
            [
                'id' => 2,
                'name' => 'two',
                'children' =>
                [
                    [
                        'id' => 4,
                        'name' => 'four'
                    ]
                ]
            ],
            [
                'id' => 3,
                'name' => 'three',
                'children' =>
                 [
                    [
                        'id' => 5,
                        'name' => 'five'                        
                    ]
                ]
            ]
        ]
    ],
    [
        'id' => 6,
        'name' => 'six'
    ]   
];

$stanley = [];
$flatten = function(array $data) use (&$flatten, &$stanley) {
    foreach($data as $k => $v) {
        if(isset($v['children'])) {
            $flatten($v['children']);
            unset($v['children']);
        }
        $stanley[] = $v;
    }
};

$flatten($data);
var_export($stanley);
array (
  0 => 
  array (
    'id' => 4,
    'name' => 'four',
  ),
  1 => 
  array (
    'id' => 2,
    'name' => 'two',
  ),
  2 => 
  array (
    'id' => 5,
    'name' => 'five',
  ),
  3 => 
  array (
    'id' => 3,
    'name' => 'three',
  ),
  4 => 
  array (
    'id' => 1,
    'name' => 'one',
  ),
  5 => 
  array (
    'id' => 6,
    'name' => 'six',
  ),
)
function dropdownNavigationTree($array) {

$response = [];

   foreach($array as $page) {

       if (!is_array($page['children'])) { 

           $response[$page['id']] = ($page['depth'] > 0 ? str_repeat("-", $page['depth']).' ' : FALSE).$page['identifier'];

       } else {

           $response[$page['id']] = ($page['depth'] > 0 ? str_repeat("-", $page['depth']).' ' : FALSE).$page['identifier'];
           $children = dropdownNavigationTree($page['children']);

           $response = $response + $children;

       }

   }

   return $response;

}