更新时间:2022-06-06 23:37:41
我不认为这是在PHP中的内置函数,它这一点。
I don't think there is a built-in function in PHP that does this.
我尝试以下code,它似乎工作,prepare嵌套数组你描述的方式:
I tried the following code, and it seems to work to prepare the nested array the way you describe:
$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);
我在一个PHP类我在SQL中我的presentation 分层模型中写道写了一个类似的算法和PHP ,但我使用对象而不是简单的数组。
I wrote a similar algorithm in a PHP class I wrote for my presentation Hierarchical Models in SQL and PHP, but I was using objects instead of plain arrays.