我有一个包含树数据的数组(通过父ID)。我想将其转换为多维数组。实现这一目标的最佳方法是什么?那有什么简短的功能吗?
源数组:
$source = array(
'0' => array(
'Menu' => array(
'id' => 45
'name' => 'Home'
'parent_id' => 1
)
)
'1' => array(
'Menu' => array(
'id' => 47
'name' => 'Get started'
'parent_id' => 1
)
)
'2' => array(
'Menu' => array(
'id' => 72
'name' => 'Attributes'
'parent_id' => 71
)
)
'3' => array(
'Menu' => array(
'id' => 73
'name' => 'Headings'
'parent_id' => 71
)
)
'4' => array(
'Menu' => array(
'id' => 75
'name' => 'Links'
'parent_id' => 71
)
)
'5' => array(
'Menu' => array(
'id' => 59
'name' => 'Images'
'parent_id' => 75
)
)
'6' => array(
'Menu' => array(
'id' => 65
'name' => 'Lists'
'parent_id' => 75
)
)
);
源阵列中缺少一些父母。我希望缺少父项的项目是root。结果数组:
$result = array(
'0' => array(
'Menu' => array(
'id' => 45
'name' => 'Home'
'parent_id' => 1
)
'Children' => array()
)
'1' => array(
'Menu' => array(
'id' => 47
'name' => 'Get started'
'parent_id' => 1
)
'Children' => array()
)
'2' => array(
'Menu' => array(
'id' => 72
'name' => 'Attributes'
'parent_id' => 71
)
'Children' => array()
)
'3' => array(
'Menu' => array(
'id' => 73
'name' => 'Headings'
'parent_id' => 71
)
'Children' => array()
)
'4' => array(
'Menu' => array(
'id' => 75
'name' => 'Links'
'parent_id' => 71
)
'Children' => array(
'0' => array(
'Menu' => array(
'id' => 59
'name' => 'Images'
'parent_id' => 75
)
'Children' => array()
)
'1' => array(
'Menu' => array(
'id' => 65
'name' => 'Lists'
'parent_id' => 75
)
'Children' => array()
)
)
)
);
更新:删除方括号。
答案 0 :(得分:16)
我不认为PHP中有内置函数可以做到这一点。
我尝试了以下代码,它似乎可以按照您描述的方式准备嵌套数组:
$nodes = array();
$tree = array();
foreach ($source as &$node) {
$node["Children"] = array();
$id = $node["Menu"]["id"];
$parent_id = $node["Menu"]["parent_id"];
$nodes[$id] =& $node;
if (array_key_exists($parent_id, $nodes)) {
$nodes[$parent_id]["Children"][] =& $node;
} else {
$tree[] =& $node;
}
}
var_dump($tree);
我在为我的演示文稿Hierarchical Models in SQL and PHP编写的PHP类中编写了类似的算法,但我使用的是对象而不是普通数组。
答案 1 :(得分:0)
考虑到root parent_id为0或缺少,我写了这个变体。无论是父母在DB($ source)之后的孩子。
$source_by_id = array();
foreach ($source as &$row){
$source_by_id[$row['id']] = &$row;
}
foreach ($source_by_id as $id => &$row){
$source_by_id[ intval($row['parent_id']) ]['children'][$id] = &$row;
}
// remove cycling itself
unset($source_by_id[0]['children'][0]);
$result = $source_by_id[0]['children'];
结果数组键是适当的ID。享受!
答案 2 :(得分:0)
我正在寻找一个如何使用类别来做这个的例子。此示例假定父项的父ID始终为“0”。该示例使用ZF2。
没有引用或递归。诀窍是在输出中,您查找[0]索引,对于子项,您将parent_id指定为索引。
$categoryLookup = $this->getCategoryLookup($associateById=true);
if ($assignedCategories) {
$categoryHeirarchy = array();
foreach($assignedCategories as $assignedCategory) {
$child = $categoryLookup[$assignedCategory->category_id];
$parent = $categoryLookup[$child->parent_id];
$categoryHeirarchy[$child->parent_id][] = $categoryLookup[$child->category_id];
$categoryHeirarchy[$parent->parent_id][$parent->category_id] = $categoryLookup[$parent->category_id];
}
return $categoryHeirarchy;
}
<h3>Categories</h3>
<dl class="dl-horizontal">
<?php foreach($this->categoryHeirarchy[0] as $parent): ?>
<dt><?php echo $this->escapeHtml($parent->name); ?></dt>
<?php foreach($this->categoryHeirarchy[$parent->category_id] as $child): ?>
<dd><?php echo $this->escapeHtml($child->name); ?></dd>
<?php endforeach; ?>
<?php endforeach; ?>
</dl>