从一个转换数组的多维基于父ID值 [英] Converting an array from one to multi-dimensional based on parent ID values
问题描述
我已经得到了重新present多维数据对象的一维数组:
阵列(
阵列(
ID=> 45,
PARENT_ID=>空值
)
阵列(
ID=> 200,
PARENT_ID=> 45
)
阵列(
ID=> 345,
PARENT_ID=> 45
)
阵列(
ID=> 355
PARENT_ID=> 200
)
);
我应该如何将它转换成一个多维数组:
阵列(
阵列(
ID=> 45,
PARENT_ID=>空值,
孩子= GT;阵列(
阵列(
ID=> 200,
PARENT_ID=> 45,
孩子= GT;阵列(
ID=> 355
PARENT_ID=> 200
) )
阵列(
ID=> 345,
PARENT_ID=> 45
)
)
)
);
以下code-例子的转换的数组 $阵列
成树结构你要找的:
//键数组按id
$键控=阵列();
的foreach($数组和放大器; $值)
{
$键入[$值['身份证'] =安培; $价值;
}
未设置($值);
$阵列= $键控;
未设置($键控);//它的树
$树=阵列();
的foreach($数组和放大器; $值)
{
如果($父= $值['PARENT_ID'])
$阵列[$父] ['孩子'] [] =&安培; $价值;
其他
$树[] =&安培; $价值;
}
未设置($值);
$阵列= $树;
未设置($树);后续代码var_dump($数组); #你的结果
这是不行的,如果有一个的现有的父的ID 0
。但是,可以很容易地改变,以反映这一点。
这是一个相关的问题,具有原始数组已经键控,因此解决方案的前半部分可以幸免:<一href=\"http://stackoverflow.com/questions/7673044/nested-array-third-level-is-disappearing/7673415\">Nested数组。第三个层次是消失。
编辑:
那么如何工作的呢?这是利用PHP变量走样(也称为引用的)和所使用到)别名存储节点( $键入
)和b)建立新的树顺序( $树(临时)阵列
)。
你能[...]说明
$阵列的目的= $键入
,$阵列= $树
和取消设置?
块引用>由于两种,
$键入
和$树
包含$数组值引用
,我先在该信息复制到$阵列
,例如:$阵列= $键控;
由于现在
$键入
仍设置(并包含相同的值引用在$阵列
),键入$
是取消设置:未设置($键控);
这未将所有的引用在
键入$
,并确保,在$阵列中的所有值
不引用不再(该值的引用计数减一)。如果临时数组不是迭代后未设置,还是会存在的引用。如果你使用
的var_dump
在$阵列
,你会看到所有的值将有一个&放;
在前面,因为它们仍然被引用。未设置($键控)
再次删除这些引用,的var_dump($数组)
,你将看到&放大器;
s的走了。我希望这是可以理解的,引用可能很难,如果你不流利与他们有时会跟随。它经常帮助我思考他们作为变量别名。
如果你想运动一下,可以考虑以下内容:
如何在
$阵列
一个foreach迭代转换平树?
块引用>决定你自己的,当你想点击其中包含一个解决方案。
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屋!