如何将2级多维数组更改为n级?

时间:2014-10-14 14:27:43

标签: php arrays multidimensional-array

我有一个如下数组:

Array
(
    [1g27] => Array
        (
            [42] => AAAAA
            [52] => BBBBB
            [4] => 2g4
        )
    [2g4] => Array
        (
            [9] => CCCCC
            [14] => 3g14
            [15] => 3g15
            [20] => DDDDD
            [34] => EEEEE
        )
    [3g14] => Array
        (
            [49] => FFFFF
        )
    [3g15] => Array
        (
            [50] => GGGGG
        )
)

我希望它成为如下数组:

Array
(
    [1g27] => Array
        (
            [42] => AAAAA
            [52] => BBBBB
            [2g4] => Array
            (
                [9] => CCCCC
                [3g14] => Array
                (
                    [49] => FFFFF
                )
                [3g15] => Array
                (
                    [50] => GGGGG
                )
                [20] => DDDDD
                [34] => EEEEE
        )
)

请注意,原始数组的值也是原始数组中的键。当值和数组匹配时,我希望数组替换该值(并重命名该值的键)。

我尝试了foreach和array_walk_recursive,但我无法弄明白。 (参见以下只有1级深度的尝试。)

function inflate($flatree, $array)
    {
    global $inflatedtree;
    foreach ($array as $arraykey => $arrayvalue)
        {
        $inflatedtree[$arraykey] = $arrayvalue;
        if (array_key_exists($arrayvalue, $flatree))
            {
            $inflatedtree[$arrayvalue] = $flatree[$arrayvalue];
            inflate($flatree, $inflatedtree[$arrayvalue]);
            }
        }
    }
inflate($flatree, $flatree['1g27']);
然而,

使用这个功能给了我这个:

Array
(
    [42] => AAAAA
    [52] => BBBBB
    [2g4] => Array
        (
            [9] => CCCCC
            [14] => 3g14
            [15] => 3g15
            [20] => DDDDD
            [34] => EEEEE
        )

    [3g14] => Array
        (
            [49] => FFFFF
        )
    [3g15] => Array
        (
            [50] => GGGGG
        )
)

2 个答案:

答案 0 :(得分:2)

我将全局变量$ inflatedtree替换为本地变量$ inflatedtree,它是函数inflate()的返回值。此返回值是新的构建分支。

function inflate($flatree, $array)
{
    $inflatedtree = array();
    foreach ($array as $arraykey => $arrayvalue) {
        if (array_key_exists($arrayvalue, $flatree)) {
            $inflatedtree[$arrayvalue] = inflate($flatree, $flatree[$arrayvalue]);
        } else {
            $inflatedtree[$arraykey] = $arrayvalue;
        }
    }
    return $inflatedtree;
}

我使用函数start()进行初始调用,以便根据需要返回数组。直接调用inflate()将返回内部数组。

function start($flatree, $key)
{
    return array($key => inflate($flatree, $flatree[$key]));
}

$result_tree = start($flatree, '1g27');

答案 1 :(得分:0)

我没有看到你的代码应该只深入一级的原因。您似乎正在正确调用递归函数。唯一的错误是密钥未重命名但复制到新名称。你需要删除像这样的旧值

function inflate($flatree, $array)
{
global $inflatedtree;
foreach ($array as $arraykey => $arrayvalue)
    {
    $inflatedtree[$arraykey] = $arrayvalue;
    if (array_key_exists($arrayvalue, $flatree))
        {
        unset($inflatedtree[$arraykey]);
        //now the new stuff is only at $arrayvalue, not $arraykey
        $inflatedtree[$arrayvalue] = $flatree[$arrayvalue];
        inflate($flatree, $inflatedtree[$arrayvalue]);
        }
    }
}
inflate($flatree, $flatree['1g27']);

如果您的代码在示例数据上运行时没有给出示例结果,那么请发布它所生成的数组。