doupian6118 2014-10-14 14:27
浏览 42
已采纳

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

I have an array like the following:

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
        )
)

I would like it to become an array like the following:

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

Notice that the values of the original array are also keys found in the original array. When there is a match between a value and an array, I want the array to replace the value (and rename that value's key).

I have tried foreach and array_walk_recursive, but I can't figure it out. (See following attempt which only goes 1 level deep.)

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']);

using this function, however, gives me this:

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条回答 默认 最新

  • doumi4974 2014-10-14 18:17
    关注

    I replaced the global variable $inflatedtree by a local one, which is the return value of the function inflate(). This return value is the new build branch.

    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;
    }
    

    I use a function start() for the initial call to get back the array as wished. Calling inflate() directly will return the inner array.

    function start($flatree, $key)
    {
        return array($key => inflate($flatree, $flatree[$key]));
    }
    
    $result_tree = start($flatree, '1g27');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下