树深度递归PHP数组
我有以下数组,希望找到其树结构的最大深度。但是我的代码返回12,当它应该是4时…我不太擅长递归,所以这有点让我发疯 数组声明:树深度递归PHP数组,php,arrays,recursion,tree,depth,Php,Arrays,Recursion,Tree,Depth,我有以下数组,希望找到其树结构的最大深度。但是我的代码返回12,当它应该是4时…我不太擅长递归,所以这有点让我发疯 数组声明: Array ( [relation] => Array ( [parent.item] => Array ( [0] => cs [1] => ls ) [cs.item] => Array (
Array (
[relation] => Array (
[parent.item] => Array (
[0] => cs
[1] => ls
)
[cs.item] => Array (
[0] => business
[1] => sporting_cultural
[2] => tourism
[3] => family
[4] => friend
[5] => student_family
[6] => transit
[7] => other_cases
)
[business.item] => Array (
[0] => short_stay_business
[1] => short_stay_business_tourism
[2] => short_stay_german_company
[3] => short_stay_german_company_tourism
[4] => short_stay_work_training
[5] => short_stay_work
[6] => short_stay_student_internship
[7] => exhibition
[8] => scientific_research_all
)
[exhibition.item] => Array (
[0] => short_stay_visitor_fair
[1] => short_stay_visitor_fair_tourism
[2] => short_stay_exhibitor
[3] => short_stay_exhibitor_tourism
)
[scientific_research_all.item] => Array (
[0] => short_stay_scientific_research
[1] => short_stay_scientific_research_spouse
[2] => short_stay_scientific_research_child
)
[sporting_cultural.item] => Array (
[0] => short_stay_sporting_or_cultural
)
[tourism.item] => Array (
[0] => short_stay_tourism
[1] => medical_treatment
)
[medical_treatment.item] => Array (
[0] => short_stay_medical_treatment
[1] => short_stay_medical_treatment_tourism_friend_family_visit
[2] => short_stay_accompanying_person_of_a_medical_patient
)
[friend.item] => Array (
[0] => short_stay_friends
)
[family.item] => Array (
[0] => short_stay_family
[1] => short_stay_german_family_in_germany
[2] => short_stay_german_family_in_china
[3] => short_stay_non_german_family
)
[student_family.item] => Array (
[0] => short_stay_student
[1] => short_stay_entrance_exam
[2] => short_stay_scholar_exchange
[3] => short_stay_student_internship
)
[transit.item] => Array (
[0] => transit_transit
[1] => airport_transit_airport_transit
)
[other_cases.item] => Array (
[0] => short_stay_seaman
)
[ls.item] => Array (
[0] => ls_notification
)
[children] => Array()
)
)
function plotTree($arr, $indent=0, $mother_run=true){
global $ini_array;
global $depth;
global $maxDepth;
if ($mother_run) {
// the beginning of plotTree. We're at rootlevel
echo "start\n";
}
foreach ($arr as $key => $value) {
if (is_array($value)) {
foreach ($value as $subKey => $subValue) {
if(in_array($subValue.".item", array_keys($ini_array['relation']))) {
$depth +=1;
plotTree($ini_array['relation'][$subValue.".item"],0,false);
}
}
$maxDepth = $maxDepth < $depth ? $depth : $maxDepth;
}
}
if ($mother_run) {
echo "end\n";
}
}
递归函数:
Array (
[relation] => Array (
[parent.item] => Array (
[0] => cs
[1] => ls
)
[cs.item] => Array (
[0] => business
[1] => sporting_cultural
[2] => tourism
[3] => family
[4] => friend
[5] => student_family
[6] => transit
[7] => other_cases
)
[business.item] => Array (
[0] => short_stay_business
[1] => short_stay_business_tourism
[2] => short_stay_german_company
[3] => short_stay_german_company_tourism
[4] => short_stay_work_training
[5] => short_stay_work
[6] => short_stay_student_internship
[7] => exhibition
[8] => scientific_research_all
)
[exhibition.item] => Array (
[0] => short_stay_visitor_fair
[1] => short_stay_visitor_fair_tourism
[2] => short_stay_exhibitor
[3] => short_stay_exhibitor_tourism
)
[scientific_research_all.item] => Array (
[0] => short_stay_scientific_research
[1] => short_stay_scientific_research_spouse
[2] => short_stay_scientific_research_child
)
[sporting_cultural.item] => Array (
[0] => short_stay_sporting_or_cultural
)
[tourism.item] => Array (
[0] => short_stay_tourism
[1] => medical_treatment
)
[medical_treatment.item] => Array (
[0] => short_stay_medical_treatment
[1] => short_stay_medical_treatment_tourism_friend_family_visit
[2] => short_stay_accompanying_person_of_a_medical_patient
)
[friend.item] => Array (
[0] => short_stay_friends
)
[family.item] => Array (
[0] => short_stay_family
[1] => short_stay_german_family_in_germany
[2] => short_stay_german_family_in_china
[3] => short_stay_non_german_family
)
[student_family.item] => Array (
[0] => short_stay_student
[1] => short_stay_entrance_exam
[2] => short_stay_scholar_exchange
[3] => short_stay_student_internship
)
[transit.item] => Array (
[0] => transit_transit
[1] => airport_transit_airport_transit
)
[other_cases.item] => Array (
[0] => short_stay_seaman
)
[ls.item] => Array (
[0] => ls_notification
)
[children] => Array()
)
)
function plotTree($arr, $indent=0, $mother_run=true){
global $ini_array;
global $depth;
global $maxDepth;
if ($mother_run) {
// the beginning of plotTree. We're at rootlevel
echo "start\n";
}
foreach ($arr as $key => $value) {
if (is_array($value)) {
foreach ($value as $subKey => $subValue) {
if(in_array($subValue.".item", array_keys($ini_array['relation']))) {
$depth +=1;
plotTree($ini_array['relation'][$subValue.".item"],0,false);
}
}
$maxDepth = $maxDepth < $depth ? $depth : $maxDepth;
}
}
if ($mother_run) {
echo "end\n";
}
}
函数绘图树($arr,$indent=0,$mother\u run=true){
全局$ini_数组;
全球美元深度;
全球$maxDepth;
如果($mother_run){
//绘图树的开始。我们在根级别
回显“开始\n”;
}
foreach($arr作为$key=>$value){
if(是_数组($value)){
foreach($subKey=>$subValue形式的值){
if(在数组中($subValue.“.item”,数组键($ini\u数组['relation'])){
$depth+=1;
plotTree($ini_数组['relation'][$subValue..item”],0,false);
}
}
$maxDepth=$maxDepth<$depth?$depth:$maxDepth;
}
}
如果($mother_run){
回显“结束\n”;
}
}
[Update}我不想找到维度的数量。在上面的示例中,树结构如下:parent=>cs=>business=>experience,因此我看到您试图根据平面数组中的“关系”而不是树的实际深度来测量深度 下面是我能想到的一些解释:
<?php
function getMaxDepth(array $arr, array $relations = array()) {
if (empty($relations)) {
return array_reduce($arr, function ($result, $elem) use ($arr) {
return max($result, getMaxDepth($elem, $arr));
});
}
// Return 1 + the depth of the deepest nested tree
return 1 + array_reduce($arr, function ($max, $elem) use ($relations) {
// If the field is not related to another field return the current max
if (!in_array($elem . '.item', array_keys($relations))) {
return $max;
}
// Return maximum of current maximum and the depth of related tree
return max($max, getMaxDepth($relations[$elem . '.item'], $relations));
}, 0);
}
因此,首先,您应该避免使用全局变量或静态变量,尤其是在处理递归时。相反,我们希望通过将其作为函数参数传递来保留堆栈中所需的所有内容
第一个if
语句将getMaxDepth()
分别应用于每个子树,因为我们希望确保涵盖所有子树
然后,如果有一个或0,我们只需返回最深的“嵌套”树的深度,并为当前元素添加一个
希望这对你将来有帮助。
为什么你在递归过程中访问<代码> $iNixAlxy/Cuth>?在你爬的时候把这些孩子传给孩子是不是更有意义?也考虑一个不同的名字:<代码> $iNixAlgs,因为它看起来太像<代码>函数,这很混乱。我不想找到维度的数量。在上面的示例中,树结构如下:parent=>cs=>business=>exhibition@TimOgilvy谢谢你的编辑!现在看起来干净多了!我的荣幸。我绝对能从视觉上理解递归结构,所以我无法理解你所说的我们一直在做,直到我可以可视化结构。希望这能帮助你找到你需要的答案:)哇,谢谢,已经做了很长时间了。我会看一看并试着理解它。谢谢!数组实际上是平面的,还是使用JSON中的对象文字,这从PHP 5.4开始就可用?我真的无法解释陆上通信线。