通过数组递归迭代

时间:2016-05-10 13:58:51

标签: php arrays multidimensional-array

更新:我有一个解决方案 - 请参阅下面的详细信息。

我有一个数组,其中键是关卡(例如在导航树中) - 类似于

Array
(
    [0] => Array
    (
        [100] => Array
            (
                [name] => foo100
                [slug] => foo100
                [id] => 100
                [parent] => 0
                [level] => 0
            )

        [101] => Array
            (
                [name] => foo101
                [slug] => foo101
                [id] => 101
                [parent] => 0
                [level] => 0
            )

    )

[1] => Array
    (
        [200] => Array
            (
                [name] => foo200
                [slug] => foo200
                [id] => 200
                [parent] => 100
                [level] => 1
            )

        [201] => Array
            (
                [name] => foo201
                [slug] => foo201
                [id] => 201
                [parent] => 101
                [level] => 1
            )

    )

[2] => Array
    (
        [300] => Array
            (
                [name] => foo300
                [slug] => foo300
                [id] => 300
                [parent] => 200
                [level] => 2
            )

        [301] => Array
            (
                [name] => foo301
                [slug] => foo301
                [id] => 301
                [parent] => 201
                [level] => 2
            )

    )

[3] => Array
    (
        [400] => Array
            (
                [name] => foo400
                [slug] => foo400
                [id] => 400
                [parent] => 300
                [level] => 3
            )

    )

[4] => Array
    (
        [500] => Array
            (
                [name] => foo500
                [slug] => foo500
                [id] => 500
                [parent] => 400
                [level] => 4
            )

    )

)

我需要创建一个数组,从最顶层迭代并创建一个数组,其中键是该级别的slug - 产生以下内容:

Array
(
[foo500] => Array
    (
        [4] => Array
            (
                [name] => foo500
            )

        [3] => Array
            (
                [name] => foo400
            )

        [2] => Array
            (
                [name] => foo300
            )

        [1] => Array
            (
                [name] => foo200
            )

        [0] => Array
            (
                [name] => foo100
            )

    )

[foo400] => Array
    (
        [3] => Array
            (
                [name] => foo400
            )

        [2] => Array
            (
                [name] => foo300
            )

        [1] => Array
            (
                [name] => foo200
            )

        [0] => Array
            (
                [name] => foo100
            )

    )

[foo300] => Array
    (
        [2] => Array
            (
                [name] => foo300
            )

        [1] => Array
            (
                [name] => foo200
            )

        [0] => Array
            (
                [name] => foo100
            )

    )

[foo301] => Array
    (
        [2] => Array
            (
                [name] => foo301
            )

        [1] => Array
            (
                [name] => foo201
            )

        [0] => Array
            (
                [name] => foo101
            )

    )

[foo200] => Array
    (
        [1] => Array
            (
                [name] => foo200
            )

        [0] => Array
            (
                [name] => foo100
            )

    )

[foo201] => Array
    (
        [1] => Array
            (
                [name] => foo201
            )

        [0] => Array
            (
                [name] => foo101
            )

    )

[foo100] => Array
    (
        [0] => Array
            (
                [name] => foo100
            )

    )

[foo101] => Array
    (
        [0] => Array
            (
                [name] => foo101
            )

    )

)

我希望这可以解释这个问题 - 努力做到这一点!任何帮助非常感谢!

更新 - 解决方案。

为此我删除了第一级密钥

Array
(
[107] => Array
    (
        [id] => 107
        [name] => About Us
        [indexID] => about
        [level] => 0
        [parent] => 0
    )

[109] => Array
    (
        [id] => 109
        [name] => Home
        [indexID] => index
        [level] => 0
        [parent] => 0
    )
}

等等

假设$ data是上面的数组:

foreach ($data as $k => $v) {
if ($v['parent'] == 0) {
    $bc[$v['indexID']][0]['name'] = $v['name'];
    $bc[$v['indexID']][0]['indexID'] = $v['indexID'];
}
else {
    $nextParent = $v['parent'];
    $currentIndexID = $v['indexID'];
    $currentName = $v['name'];
    $bc[$v['indexID']][0]['name'] = $currentName;
    $bc[$v['indexID']][0]['indexID'] = $currentIndexID;

    for($i=1;$i<=$level;$i++) {
        foreach ($data as $a => $b) {
            if ($a == $nextParent) {
                $nextParent = $b['parent'];
                $bc[$v['indexID']][$i]['name'] = $b['name'];
                $bc[$v['indexID']][$i]['indexID'] = $b['indexID'];
            }
        }
    }
}
}

0 个答案:

没有答案