我有以下结构:
Array
(
[0] => Array
(
[id] => 1
[children] => Array
(
[0] => Array
(
[id] => 5
)
[1] => Array
(
[id] => 7
)
[2] => Array
(
[id] => 8
[children] => Array
(
[0] => Array
(
[id] => 10
)
[1] => Array
(
[id] => 4
)
)
)
[3] => Array
(
[id] => 6
)
[4] => Array
(
[id] => 3
)
[5] => Array
(
[id] => 2
)
[6] => Array
(
[id] => 9
)
)
)
[1] => Array
(
[id] => 13
)
)
我想知道的是一个关键的根结构。 换句话说。
我们有ID号10.我想知道根结构(至少我知道如何描述它)
结果应为:
$result = array(
0 => 1,
1 => 8
);
但我不知道如何实现这一目标。也不怎么开始。 我唯一能想到的就是创建几个包含所有子节点的子数组,然后再次运行数组并检查ID是否可用。但是在结构复杂的情况下。就速度而言,这几乎是无法解决的。
我希望有人知道这个问题的解决方案。
很好,这篇文章被低估了,因为人们认为这太简单了。但我仍在寻找答案。递归函数对我没用。
///########-------------------------------------------------------------
///########-------------------------------------------------------------
///######## FUNCTION TO GET THE PAGE ORDER
///########-------------------------------------------------------------
///########-------------------------------------------------------------
private function GetPageLocation($PageArray, $PreFix = array()){
///######## RETURN THE COLLECTED DATA
foreach($PageArray as $key => $value){
///######## IF THE PAGE HAS BEEN SET
if(isset($value['id']) === true){
echo PHP_EOL.$value['id'];
///######## IF THE ID IS NOT THE CURRENT ID
if($value['id'] != $this->PageID){
///######## ADD THE PREFIX
$PreFix[] = $value['id'];
}
///######## IF THE ID IS THE CURRENT ID
else{
exit('sdd');
///######## HALT THE LOOP
break;
}
}
///######## IF THE VALUE IS AN ARRAY
if(isset($value['children']) === true){
///######## EXECUTE HIMSELF
$PreFix = array_merge($PreFix, $this->GetPageLocation($value, $PreFix));
}
///######## IF THE VALUE IS NO ARRAY
else{
}
}
///########==================================================
///######## RETURN THE PREFIX
///########==================================================
return($PreFix);
///########==================================================
}