根据父 ID 值将数组从一维转换为多维 [英] Converting an array from one to multi-dimensional based on parent ID values

查看:68
本文介绍了根据父 ID 值将数组从一维转换为多维的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表示多维数据的一维对象数组:

数组(大批(身份证"=>45,parent_id" =>空值),大批(身份证"=>200,parent_id" =>45),大批(身份证"=>345,parent_id" =>45),大批(身份证"=>"355",parent_id" =>200));

我应该如何将其转换为多维数组:

数组(大批(身份证"=>45,parent_id" =>空值,孩子" =>大批(大批(身份证"=>200,parent_id" =>45,孩子" =>大批(身份证"=>"355",parent_id" =>200)),大批(身份证"=>345,parent_id" =>45),)),);

解决方案

以下代码示例数组 $array 转换为您正在查找的树结构对于:

//按 id 键值数组$keyed = array();foreach($array 作为 &$value){$keyed[$value['id']] = &$value;}未设置($值);$array = $keyed;未设置($keyed);//树它$tree = 数组();foreach($array 作为 &$value){如果 ($parent = $value['parent_id'])$array[$parent]['children'][] = &$value;别的$tree[] = &$value;}未设置($值);$array = $tree;未设置($树);var_dump($array);# 你的结果

如果存在 0现有 父 ID,这将不起作用.但可以轻松更改以反映这一点.

这是一个相关的问题,原始数组已经被键入,所以解决方案的前半部分可以省去:嵌套数组.第三层正在消失.

那么这是如何工作的呢?这是利用 PHP 变量别名(也称为 references)和(临时)数组,用于存储 a) 节点的别名 ($keyed) 和 b) 以构建新的树顺序 ($tree).<块引用>

你能[...]解释一下 $array = $keyed$array = $tree 和 unsets 的目的吗?

由于 $keyed$tree 都包含对 $array 中值的引用,我首先将该信息复制到 $array,例如:

$array = $keyed;

由于现在 $keyed 仍然设置(并且包含对与 $array 中相同值的引用),$keyed 未设置:

unset($keyed);

这会取消设置 $keyed 中的所有引用,并确保 $array 中的所有值都不再被引用(该值的引用计数减一).

如果迭代后临时数组没有取消设置,它们的引用仍然存在.如果您在 $array 上使用 var_dump,您会看到所有值前面都有一个 &,因为它们仍然被引用.unset($keyed) 删除这些引用,再次 var_dump($array),您将看到 & 消失了.>

我希望这是可以理解的,如果您不熟悉参考文献,有时可能很难理解.我经常将它们视为变量别名.

如果您想进行一些锻炼,请考虑以下事项:

<块引用>

如何通过一次 foreach 迭代将 $array 从平面转换为树?

您自己决定何时点击包含解决方案的链接.

I've got a one-dimensional array of objects that represent multi-dimensional data:

array(
    array(
        "id" => 45,
        "parent_id" => null
    ),
    array(
        "id" => 200,
        "parent_id" => 45
    ),
    array(
        "id" => 345,
        "parent_id" => 45
    ),
    array(
        "id" => "355",
        "parent_id" => 200
    )
);

How should I convert it into a multi-dimensional array:

array(
    array(
        "id" => 45,
        "parent_id" => null,
        "children" => array(
            array(
                "id" => 200,
                "parent_id" => 45,
                "children" => array(
                    "id" => "355",
                    "parent_id" => 200
                )

            ),
            array(
                "id" => 345,
                "parent_id" => 45
            ),
        )
    ),
);

解决方案

The following code-example converts the array $array into the tree-structure you're looking for:

// key the array by id
$keyed = array();
foreach($array as &$value)
{
    $keyed[$value['id']] = &$value;
}
unset($value);
$array = $keyed;
unset($keyed);

// tree it
$tree = array();
foreach($array as &$value)
{
    if ($parent = $value['parent_id'])
        $array[$parent]['children'][] = &$value;
    else
        $tree[] = &$value;
}
unset($value);
$array = $tree;
unset($tree);

var_dump($array); # your result

This does not work, if there is an existing parent id that is 0. But could be easily changed to reflect that.

This is a related question, that has the original array already keyed, so the first half of the solution could be spared: Nested array. Third level is disappearing.

Edit:

So how does this work? This is making use of PHP variable aliasing (also known as references) and (temporary) arrays that are used to store a) aliases to the nodes ($keyed) and b) to build the new tree order ($tree).

Could you [...] explain the purpose of $array = $keyed, $array = $tree and the unsets?

As both, $keyed and $tree contain references to values in $array, I first copy over that information into $array, e.g.:

$array = $keyed;

As now $keyed is still set (and contains references to the same values as in $array), $keyed is unset:

unset($keyed);

This un-sets all references in $keyed and ensures, that all values in $array aren't referenced any longer (the value's refcount is reduced by one).

If the temporary arrays are not unset after the iteration, their references would still exist. If you use var_dump on $array, you would see that all values would have a & in front, because they are still referenced. unset($keyed) removes these references, var_dump($array) again, and you will see the &s are gone.

I hope this was understandable, references can be hard to follow sometimes if you're not fluent with them. It often helps me to think about them as variable aliases.

If you want some exercise, consider the following:

How to convert your $array from flat to tree with one foreach iteration?

Decide on your own when you would like to click the link which contains a Solution.

这篇关于根据父 ID 值将数组从一维转换为多维的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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