合并平面数组与自身以递归方式构建多维数组

时间:2016-01-27 10:46:24

标签: php arrays recursion multidimensional-array

尝试从平面数据中获取多维数组

可用的原始数据

这个原始数据可供我使用。我需要构建一个多维数组,其中所有子节点都存储在各自的父节点中。

Array
(
    [index] => Array
    (
        [slug] => index
        [parent_slug] => 
    )
    [praxis-und-team] => Array
    (
        [slug] => praxis-und-team
        [parent_slug] => 
    )
    [leistungen] => Array
    (
        [slug] => leistungen
        [parent_slug] => praxis-und-team
    )
    [partner-und-netzwerk] => Array
    (
        [slug] => partner-und-netzwerk
        [parent_slug] => 
    )
    [notfall] => Array
    (
        [slug] => notfall
        [parent_slug] => 
    )
    [impressum] => Array
    (
        [slug] => impressum
        [parent_slug] => leistungen
    )
)

需要数据

这是关于slug / parent_slug配对的全部内容。 可能会有更多的子级,因此必须递归才能在最高级别达到parent_slug == ''。这就是输出应该是这样的:

Array
(
    [index] => Array
    (
        [slug] => index
        [parent_slug] => 
    )
    [praxis-und-team] => Array
    (
        [slug] => praxis-und-team
        [parent_slug] => 
        [children] => Array
        (
            [leistungen] => Array
            (
                [slug] => leistungen
                [parent_slug] => praxis-und-team
                [children] => Array
                (
                    [impressum] => Array
                    (
                        [slug] => impressum
                        [parent_slug] => leistungen
                    )
                )
            )
        )
    )
    [partner-und-netzwerk] => Array
    (
        [slug] => partner-und-netzwerk
        [parent_slug] => 
    )
    [notfall] => Array
    (
        [slug] => notfall
        [parent_slug] => 
    )
)

非常感谢您的帮助!对不起,如果我没有弄明白问题。我已经看了很长时间......

2 个答案:

答案 0 :(得分:1)

假设:数组中的条目是从最外层到最内层。因此,我们可以恢复原始数组,通过一次遍历,重组它,然后再次恢复它(如果需要)。

代码

    <?php
    $myarray = array(
        'index' => array(
            'slug' => 'index',
            'parent_slug' => '',
        ),
        'praxis-und-team' => array (
            'slug' => 'praxis-und-team',
            'parent_slug' => '',
        ),
        'leistungen' => array(
            'slug' => 'leistungen',
            'parent_slug' => 'praxis-und-team',
        ),
        'partner-und-netzwerk' => array (
            'slug' => 'partner-und-netzwerk',
            'parent_slug' => '',
        ),
        'notfall' => array(
            'slug' => 'notfall',
            'parent_slug' => '',
        ),
        'impressum' => array (
            'slug' => 'impressum',
            'parent_slug' => 'leistungen',
        ),
    );

    echo '<pre>'; var_dump($myarray); echo '</pre>';

    // We want to work from bottom to top
    $myarray = array_reverse($myarray, true);

    foreach($myarray as $key => $item) {
        if (!empty($myarray[$key]['parent_slug'])) {
            $myarray[$myarray[$key]['parent_slug']]['children'][$key] = $myarray[$key];
            unset($myarray[$key]);
        }
    }

    // Now reverse the array again.
    $myarray = array_reverse($myarray, true);

    echo '<pre>'; var_dump($myarray); echo '</pre>';


    ?>

输出

    array(4) {
      ["index"]=>
      array(2) {
        ["slug"]=>string(5) "index"
        ["parent_slug"]=>string(0) ""
      }
      ["praxis-und-team"]=>
      array(3) {
        ["slug"]=>string(15) "praxis-und-team"
        ["parent_slug"]=>string(0) ""
        ["children"]=>
        array(1) {
          ["leistungen"]=>
          array(3) {
            ["slug"]=>string(10) "leistungen"
            ["parent_slug"]=>string(15) "praxis-und-team"
            ["children"]=>
            array(1) {
              ["impressum"]=>
              array(2) {
                ["slug"]=>string(9) "impressum"
                ["parent_slug"]=>string(10) "leistungen"
              }
            }
          }
        }
      }
      ["partner-und-netzwerk"]=>
      array(2) {
        ["slug"]=>string(20) "partner-und-netzwerk"
        ["parent_slug"]=>string(0) ""
      }
      ["notfall"]=>
      array(2) {
        ["slug"]=>string(7) "notfall"
        ["parent_slug"]=>string(0) ""
      }
    }

答案 1 :(得分:0)

我找到了一个solution,并且稍微调整了一下我的数据:

function buildTree(array &$elements, $parentId = '')
{
    $branch = array();
    foreach($elements as &$element) {
        if ($element['parent_slug'] == $parentId) {
            $children = buildTree($elements, $element['slug']);
            if ($children)
                $element['children'] = $children;
            $branch[$element['slug']] = $element;
            unset($element);
        }
    }
    return $branch;
}