递归函数,用于从分层数组生成平面(但标题嵌套)数组


Recursive function to generate flat (but with titles nested) array from hierarchical array

另一个递归问题。

在过去的几个小时里,我试图开发的是一个函数,它可以让我生成一个相同页面的平面数组,但孩子们面前有一个标识符(即"— 子页面,— — 子页面等"(。

我有一个分层的页面数组,每个子页面都嵌套在其父页面内。下面是它们的输出示例:

Array
(
    [0] => Array
        (
            [id] => 1
            [parent_id] => 0
            [title] => Parent Page
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 12
                            [parent_id] => 1
                            [title] => Another Sub Page
                        )
                    [1] => Array
                        (
                            [id] => 3
                            [parent_id] => 1
                            [title] => Sub Page
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 7
                                            [parent_id] => 3
                                            [title] => Sub Sub Page
                                        )
                                )
                        )
                )
        )
    [1] => Array
        (
            [id] => 8
            [parent_id] => 0
            [title] => Another Parent Page
        )
)

我已经能够让它工作,直到我到达递归的第二级,然后它仍然只生成一个—.我想使解决方案递归。

我在使用上面示例的最终输出中寻找的是:

Array
(
    [1] => Parent Page
    [12] => — Another Sub Page
    [3] => — Sub Page
    [7] => — — Sub Sub Page
    [8] => Another Parent Page
)
function generate_array(array $arr, &$output = array(), $index = 0)
{
    foreach($arr as $item)
    {
        $output[$item['id']] = str_repeat('— ', $index) . $item['title'];
        if(isset($item['children']))
        {
            generate_array($item['children'], $output, $index + 1);
        }
    }
    return $output;
}
$output = generate_array($arr);

存储在$output中的数据将是:

Array
(
    [1] =>  Parent Page
    [12] => — Another Sub Page
    [3] => — Sub Page
    [7] => — — Sub Sub Page
    [8] =>  Another Parent Page
)
<?php
$output = array();
$iter = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
foreach ($iter as $key => $val) {
    if ($key === 'title') {
        $output[] = str_repeat('&mdash;', floor($iter->getDepth()/2)) . $val;
    }
}
print_r($output);

你也可以做迭代

$stack = $output = array();
$stack[] = array(0, $array);
while ($stack) {
    list($depth, $nodes) = array_pop($stack);
    foreach ($nodes as $node) {
        if (isset($node['children'])) {
            $stack[] = array($depth + 1, $node['children']);
        }
        $output[] = str_repeat('&mdash;', $depth) . $node['title'];
    }
}
print_r($output);

值得一提的是,尽管它们的名字,递归迭代器实际上并没有在内部使用递归。他们管理类似于我的第二个示例的堆栈。

我使用了这样的示例数组:

$array = array(
    array(
        'title' => 'Test',
        'children' => array(
            array(
                'title' => 'test child',
                'children' => array(
                    array(
                        'title' => 'yo dawg, i heard you like recursion!'
                    )
                )
            )
        )
    ),
    array(
        'title' => 'Test 2',
        'children' => array()
    )
);

功能是这样的:

function flatIt($array, $depth = 0, &$flat = array())
{    
    foreach ($array as $item) {
        $flat[] = array('title' => str_repeat('&mdash;', $depth) . $item['title']);
        if (!empty ($item['children'])) {
            flatIt($item['children'], $depth + 1, $flat);
        }
    }
    return $flat;
}

当你像这样调用函数时:

$result = flatIt($array);
var_dump($result);

您将获得以下结果:

array
  0 => 
    array
      'title' => string 'Test' (length=4)
  1 => 
    array
      'title' => string '&mdash;test child' (length=17)
  2 => 
    array
      'title' => string '&mdash;&mdash;yo dawg, i heard you like recursion!' (length=50)
  3 => 
    array
      'title' => string 'Test 2' (length=6)