嵌套数组。第三个层次是消失 [英] Nested array. Third level is disappearing

查看:137
本文介绍了嵌套数组。第三个层次是消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组:

$a = array(
    "7" => array(
        "id" => 7,
        "parent" => 6
    ),
    "6" => array(
        "id" => 6,
        "parent" => 5
    ),
    "5" => array(
        "id" => 5,
        "parent" => 4
    ),
    "4" => array(
        "id" => 4,
        "parent" => 0
    ),
    "3" => array(
        "id" => 7,
        "parent" => 2
    ),
    "2" => array(
        "id" => 7,
        "parent" => 1
    ),
    "1" => array(
        "id" => 7,
        "parent" => 0
    )
);

这是我想要的结果是:

$a = array(
    "4" => array(
        "id" => 4,
        "parent" => 0,
        array(
            "5" => array(
                "id" => 5,
                "parent" => 4,
                array(
                    "6" => array(
                        "id" => 6,
                        "parent" => 5,
                        array(
                            "7" => array(
                                "id" => 7,
                                "parent" => 6
                            )
                        )
                    )
                )
            )
        )
    ),
    "2" => array(
        "id" => 7,
        "parent" => 1,
        array(
            "3" => array(
                "id" => 7,
                "parent" => 2
            )
        )
    ),
    "1" => array(
        "id" => 7,
        "parent" => 0
    )
);

这是我使用code是这样的:

the code that I use is this :

foreach($a as $v)
{
    if(isset($a[$v['PARENT']]))
    {
        $a[$v['PARENT']][$v['ID']] = $v;
        unset($a[$v['ID']]);
    }
}

和我有问题是,我得到的结果是:

and the problem that I have is that I get that result :

$a = array(
    "4" => array(
        "id" => 4,
        "parent" => 0,
        array(
            "5" => array(
                "id" => 5,
                "parent" => 4
            )
        )
    ),
    "2" => array(
        "id" => 7,
        "parent" => 1,
        array(
            "3" => array(
                "id" => 7,
                "parent" => 2
            )
        )
    ),
    "1" => array(
        "id" => 7,
        "parent" => 0
    )
);

,而不是它导致的需要。

instead of the need it result.

推荐答案

要解决你的问题,你需要正确地理解如何的变量引用/别名在PHP 工作。

To solve your problem you need to properly understand how variable referencing/aliasing in PHP works.

请看下面的例子code,它不看你要多大不同,但利用引用才能访问,即使它已经搬任何父母:

Look at the following example code, which does not look much different to yours but makes use of references in order to access any parent even it has already "moved":

# transform $flat into a tree:
foreach($flat as $id => &$value)
{
    # check if there is a parent
    if ($parentId = $value['parent'])
    {
        $flat[$parentId][0][$id] =& $value; # add child to parent
        unset($flat[$id]); # remove reference from topmost level
    }
}
unset($value); # remove iterator reference
print_r($flat); # your tree

$平现在包含 $平所有值 - 但重新排序。 演示

$flat now contains all values from $flat - but reordered. Demo.

这篇关于嵌套数组。第三个层次是消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆