如何在不使用引用的情况下从平面数组生成树状数组

时间:2015-07-28 11:34:30

标签: php arrays

我坚持将平面数组转换为多维树状。 I have already done it, but I used references, which creates another set of problems down the line,所以我需要在没有参考的情况下这样做。

输入数组:

Array
[
1 =>[
        'content_id' => 1,
        'sort_order' => 1,
        'level' => 1
    ],
2 =>[
        'content_id' => 7,
        'sort_order' => 2,
        'level' => 2
    ],
3 =>[
        'content_id' => 4,
        'sort_order' => 3,
        'level' => 2
    ],
4 =>[
        'content_id' => 2,
        'sort_order' => 4,
        'level' => 3
    ],
5 =>[
        'content_id' => 3,
        'sort_order' => 5,
        'level' => 1
    ],
6 =>[
        'content_id' => 6,
        'sort_order' => 6,
        'level' => 1
    ],
7 =>[
        'content_id' => 5,
        'sort_order' => 7,
        'level' => 2
    ]
]

输出数组:

1 => [
    'id' = 1,
    'visited' = 0,
    'children' => [
             2 => [
                  'id' => 7,
                  'visited' => 0,
                  'children' => []
             ],
             3 => [
                  'id' => 4,
                  'visited' => 0,
                  'children' => [
                           4 => [
                                'id' = 2,
                                'visited' = 0,
                                'children' => []
                           ]
             ]
],
5 => [
     'id' => 3,
     'visited' => 0,
     'children' => []
],
6 => [
     'id' => 6,
     'visited' => 0,
     'children' => [
             7 => [
                  'id' => 5,
                  'visited' => 0,
                  'children => []
             ]
]
]

任何想法如何在没有直接父关系设置的情况下解决这样的问题?我可以使用递归,但引用是有问题的。

1 个答案:

答案 0 :(得分:1)

噢,这种问题确实让我感到害怕。所以,这是我的解决方案:

<?php

$origArray = array(
    array('content_id' => 1, 'sort_order' => 1, 'level' => 1),
    array('content_id' => 7, 'sort_order' => 2, 'level' => 2),
    array('content_id' => 4, 'sort_order' => 3, 'level' => 2),
    array('content_id' => 2, 'sort_order' => 4, 'level' => 3),
    array('content_id' => 3, 'sort_order' => 5, 'level' => 1),
    array('content_id' => 6, 'sort_order' => 6, 'level' => 1),
    array('content_id' => 5, 'sort_order' => 7, 'level' => 2),
);

function sortByOrder($a, $b) {
    if ($a['sort_order'] == $b['sort_order']) {
        return 0;
    }
    return ($a['sort_order'] < $b['sort_order']) ? -1 : 1;
}

function createHierarchicalArray($arr) {
    $result = array();
    foreach ($arr as $el) {
        $result = insertArrayElement($result, $el['content_id'], $el['level']);
    }
    return $result;
}

function insertArrayElement($array, $id, $level, $currentLevel = 1) {
    if ($level > $currentLevel) {
        $ids = array_keys($array);
        $currentId = end($ids);
        if (!isset($array[$currentId]['children'])) {
            $array[$currentId]['children'] = array();
        }
        $array[$currentId]['children'] = insertArrayElement($array[$currentId]['children'], $id, $level, $currentLevel + 1);
    } else {
        $array[$id] = array();
    }

    return $array;
}

// Could do without this, if the array is already sorted. Otherwise it's a necessary step.
uasort($origArray, 'sortByOrder');

$result = createHierarchicalArray($origArray);

var_dump($result);

编辑:更改了代码以包含问题中的更改。