平面数组到嵌套数组

时间:2015-07-30 00:03:28

标签: php arrays recursion nested traversal

我有一个数组如下,是否有任何内置库或开源将路径/深度转换为嵌套数组?可以有x深度,但现在只有2个。我只是拿走{}并爆炸路径并执行递归功能吗?如果那样的情况不会更容易只检索父ID,那么深度用途是什么?

Array
(
    [0] => Array
        (
            [id] => 1
            [message] => In hac habitasse platea dictumst
            [author] => 
            [path] => {1}
            [depth] => 1
        )

    [1] => Array
        (
            [id] => 19
            [message] => Pellentesque et sapien pulvinar consectetur
            [author] => 
            [path] => {1,19}
            [depth] => 2
        )

    [2] => Array
        (
            [id] => 20
            [message] => Eros diam egestas libero eu vulputate risus
            [author] => 
            [path] => {1,20}
            [depth] => 2
        )

    [3] => Array
        (
            [id] => 73
            [message] => Lorem ipsum dolor sit amet consectetur adipiscing elit
            [author] => 
            [path] => {1,73}
            [depth] => 2
        )

    [4] => Array
        (
            [id] => 2
            [message] => Pellentesque vitae velit ex
            [author] => 
            [path] => {2}
            [depth] => 1
        )

    [5] => Array
        (
            [id] => 17
            [message] => Lorem ipsum dolor sit amet consectetur adipiscing elit
            [author] => 
            [path] => {2,17}
            [depth] => 2
        )

    [6] => Array
        (
            [id] => 50
            [message] => Ut eleifend mauris et risus ultrices egestas
            [author] => 
            [path] => {2,50}
            [depth] => 2
        )

    [7] => Array
        (
            [id] => 57
            [message] => Eros diam egestas libero eu vulputate risus
            [author] => 
            [path] => {2,57}
            [depth] => 2
        )

    [8] => Array
        (
            [id] => 82
            [message] => Nulla porta lobortis ligula vel egestas
            [author] => 
            [path] => {2,82}
            [depth] => 2
        )

    [9] => Array
        (
            [id] => 87
            [message] => Ut suscipit posuere justo at vulputate
            [author] => 
            [path] => {2,87}
            [depth] => 2
        )

    [10] => Array
        (
            [id] => 96
            [message] => Lorem ipsum dolor sit amet consectetur adipiscing elit
            [author] => 
            [path] => {2,96}
            [depth] => 2
        )

    [11] => Array
        (
            [id] => 3
            [message] => Pellentesque et sapien pulvinar consectetur
            [author] => 
            [path] => {3}
            [depth] => 1
        )

    [12] => Array
        (
            [id] => 25
            [message] => Aliquam sodales odio id eleifend tristique
            [author] => 
            [path] => {3,25}
            [depth] => 2
        )

    [13] => Array
        (
            [id] => 27
            [message] => In hac habitasse platea dictumst
            [author] => 
            [path] => {3,27}
            [depth] => 2
        )

    [14] => Array
        (
            [id] => 36
            [message] => Lorem ipsum dolor sit amet consectetur adipiscing elit
            [author] => 
            [path] => {3,36}
            [depth] => 2
        )

    [15] => Array
        (
            [id] => 40
            [message] => Urna nisl sollicitudin id varius orci quam id turpis
            [author] => 
            [path] => {3,40}
            [depth] => 2
        )

    [16] => Array
        (
            [id] => 62
            [message] => Aliquam sodales odio id eleifend tristique
            [author] => 
            [path] => {3,62}
            [depth] => 2
        )

    [17] => Array
        (
            [id] => 66
            [message] => Eros diam egestas libero eu vulputate risus
            [author] => 
            [path] => {3,66}
            [depth] => 2
        )

    [18] => Array
        (
            [id] => 76
            [message] => Ut suscipit posuere justo at vulputate
            [author] => 
            [path] => {3,76}
            [depth] => 2
        )

    [19] => Array
        (
            [id] => 4
            [message] => Ut suscipit posuere justo at vulputate
            [author] => 
            [path] => {4}
            [depth] => 1
        )

    [20] => Array
        (
            [id] => 38
            [message] => Pellentesque vitae velit ex
            [author] => 
            [path] => {4,38}
            [depth] => 2
        )

    [21] => Array
        (
            [id] => 53
            [message] => Ut suscipit posuere justo at vulputate
            [author] => 
            [path] => {4,53}
            [depth] => 2
        )

    [22] => Array
        (
            [id] => 54
            [message] => Ut eleifend mauris et risus ultrices egestas
            [author] => 
            [path] => {4,54}
            [depth] => 2
        )

    [23] => Array
        (
            [id] => 72
            [message] => Mauris dapibus risus quis suscipit vulputate
            [author] => 
            [path] => {4,72}
            [depth] => 2
        )

    [24] => Array
        (
            [id] => 74
            [message] => In hac habitasse platea dictumst
            [author] => 
            [path] => {4,74}
            [depth] => 2
        )

    [25] => Array
        (
            [id] => 5
            [message] => Pellentesque et sapien pulvinar consectetur
            [author] => 
            [path] => {5}
            [depth] => 1
        )

    [26] => Array
        (
            [id] => 26
            [message] => Mauris dapibus risus quis suscipit vulputate
            [author] => 
            [path] => {5,26}
            [depth] => 2
        )

    [27] => Array
        (
            [id] => 52
            [message] => Morbi tempus commodo mattis
            [author] => 
            [path] => {5,52}
            [depth] => 2
        )

    [28] => Array
        (
            [id] => 58
            [message] => Nulla porta lobortis ligula vel egestas
            [author] => 
            [path] => {5,58}
            [depth] => 2
        )

    [29] => Array
        (
            [id] => 64
            [message] => Ut suscipit posuere justo at vulputate
            [author] => 
            [path] => {5,64}
            [depth] => 2
        )

    [30] => Array
        (
            [id] => 68
            [message] => Lorem ipsum dolor sit amet consectetur adipiscing elit
            [author] => 
            [path] => {5,68}
            [depth] => 2
        )

    [31] => Array
        (
            [id] => 71
            [message] => Pellentesque et sapien pulvinar consectetur
            [author] => 
            [path] => {5,71}
            [depth] => 2
        )

    [32] => Array
        (
            [id] => 81
            [message] => Pellentesque et sapien pulvinar consectetur
            [author] => 
            [path] => {5,81}
            [depth] => 2
        )

    [33] => Array
        (
            [id] => 95
            [message] => Pellentesque vitae velit ex
            [author] => 
            [path] => {5,95}
            [depth] => 2
        )

    [34] => Array
        (
            [id] => 6
            [message] => Nulla porta lobortis ligula vel egestas
            [author] => 
            [path] => {6}
            [depth] => 1
        )

    [35] => Array
        (
            [id] => 32
            [message] => Sed varius a risus eget aliquam
            [author] => 
            [path] => {6,32}
            [depth] => 2
        )

    [36] => Array
        (
            [id] => 49
            [message] => Lorem ipsum dolor sit amet consectetur adipiscing elit
            [author] => 
            [path] => {6,49}
            [depth] => 2
        )

    [37] => Array
        (
            [id] => 88
            [message] => Nulla porta lobortis ligula vel egestas
            [author] => 
            [path] => {6,88}
            [depth] => 2
        )

    [38] => Array
        (
            [id] => 89
            [message] => Mauris dapibus risus quis suscipit vulputate
            [author] => 
            [path] => {6,89}
            [depth] => 2
        )

    [39] => Array
        (
            [id] => 97
            [message] => Nunc viverra elit ac laoreet suscipit
            [author] => 
            [path] => {6,97}
            [depth] => 2
        )

    [40] => Array
        (
            [id] => 7
            [message] => Eros diam egestas libero eu vulputate risus
            [author] => 
            [path] => {7}
            [depth] => 1
        )

    [41] => Array
        (
            [id] => 21
            [message] => Nulla porta lobortis ligula vel egestas
            [author] => 
            [path] => {7,21}
            [depth] => 2
        )

    [42] => Array
        (
            [id] => 23
            [message] => Aliquam sodales odio id eleifend tristique
            [author] => 
            [path] => {7,23}
            [depth] => 2
        )

    [43] => Array
        (
            [id] => 29
            [message] => Curabitur aliquam euismod dolor non ornare
            [author] => 
            [path] => {7,29}
            [depth] => 2
        )

    [44] => Array
        (
            [id] => 41
            [message] => Ut eleifend mauris et risus ultrices egestas
            [author] => 
            [path] => {7,41}
            [depth] => 2
        )

    [45] => Array
        (
            [id] => 55
            [message] => Pellentesque vitae velit ex
            [author] => 
            [path] => {7,55}
            [depth] => 2
        )

    [46] => Array
        (
            [id] => 67
            [message] => Curabitur aliquam euismod dolor non ornare
            [author] => 
            [path] => {7,67}
            [depth] => 2
        )

    [47] => Array
        (
            [id] => 8
            [message] => Ut eleifend mauris et risus ultrices egestas
            [author] => 
            [path] => {8}
            [depth] => 1
        )

    [48] => Array
        (
            [id] => 30
            [message] => Ut eleifend mauris et risus ultrices egestas
            [author] => 
            [path] => {8,30}
            [depth] => 2
        )

    [49] => Array
        (
            [id] => 80
            [message] => Morbi tempus commodo mattis
            [author] => 
            [path] => {8,80}
            [depth] => 2
        )

    [50] => Array
        (
            [id] => 98
            [message] => Eros diam egestas libero eu vulputate risus
            [author] => 
            [path] => {8,98}
            [depth] => 2
        )

    [51] => Array
        (
            [id] => 9
            [message] => Aliquam sodales odio id eleifend tristique
            [author] => 
            [path] => {9}
            [depth] => 1
        )

    [52] => Array
        (
            [id] => 35
            [message] => Aliquam sodales odio id eleifend tristique
            [author] => 
            [path] => {9,35}
            [depth] => 2
        )

    [53] => Array
        (
            [id] => 46
            [message] => Ut eleifend mauris et risus ultrices egestas
            [author] => 
            [path] => {9,46}
            [depth] => 2
        )

    [54] => Array
        (
            [id] => 47
            [message] => In hac habitasse platea dictumst
            [author] => 
            [path] => {9,47}
            [depth] => 2
        )

    [55] => Array
        (
            [id] => 59
            [message] => Mauris dapibus risus quis suscipit vulputate
            [author] => 
            [path] => {9,59}
            [depth] => 2
        )
)

1 个答案:

答案 0 :(得分:0)

没有递归的样本

$array = [
    /* | */ ['id' => 1, 'depth' => 1],
    /* |----| */ ['id' => 19, 'depth' => 2],
    /* |----|----| */ ['id' => 20, 'depth' => 3],
    /* |----| */ ['id' => 73, 'depth' => 2],
    /* | */ ['id' => 3, 'depth' => 1],
    /* |----| */ ['id' => 25, 'depth' => 2],
];

$root = new stdClass();
$bread_crumbs = [$root];
foreach ($array as $item) {
    // make it object to simplify access to its parts
    $object_item = (object) $item;
    $expected_depth = count($bread_crumbs);
    if ($object_item->depth < 1) {
        throw new Exception('Depth cannot be lower than 1');
    } elseif ($object_item->depth == $expected_depth) {
        // doing nothing
    } elseif ($object_item->depth < $expected_depth) {
        // remove 'unwanted' items to make expected_depth actual
        array_splice($bread_crumbs, -($expected_depth - $object_item->depth));
    } else {
        throw new Exception('Error cannot jump too high');
    }

    end($bread_crumbs)->children[] = $object_item; // object helps here

    // and then just add to the end of bread_crumbs current object_item
    $bread_crumbs[] = $object_item;
}
var_dump($root);

https://3v4l.org/YQgR1