Recursive tutorial: a suspicious ambiguous list

Recently, an infinite classification of data was processed, mainly using recursive functions. Of course, laravel has a more elegant Association query method. However, due to the need for many other secondary modifications to the data, the original recursive processing was adopted. At the same time, an image metaphor was used to illustrate the principle of recursion:
(core idea: recursion goes and goes back)

Official description

Recursive functions are self calling functions, that is, functions call themselves directly or indirectly within the function body. It should be noted that when using a recursive function, a judgment condition is usually attached to the function body to judge whether the recursive call needs to be continued. When the condition is met, the recursive call of the function will be terminated.

Background description

Suddenly one day, your current girlfriend found that you were still in touch with some former female friends, and found an evidence in front of you to explain your relationship with them. The relevant evidence is as follows:

<?php
//source: https://www.dongyao.ren/
$arr = [
    [
        'id' =>1,
        'pid'=>0,
        'name' => 'ex-girlfriend'
    ],
    [
        'id' =>2,
        'pid'=>1,
        'name' =>'twenty fen'
    ],
    [
        'id' =>3,
        'pid'=>0,
        'name' =>'Suspicious person'
    ],
    [
        'id' =>4,
        'pid'=>2,
        'name' =>'Xiao Hong'
    ],
    [
        'id' =>5,
        'pid'=>2,
        'name' =>'Little green'
    ],
    [
        'id' =>6,
        'pid'=>3,
        'name' =>'colleague'
    ],
    [
        'id' =>7,
        'pid'=>1,
        'name' =>'Big hair'
    ],
    [
        'id' =>8,
        'pid'=>3,
        'name' =>'classmate'
    ]
];

Relevant witness

The current girlfriend spoke harshly. It might be better for you to explain the consequences yourself, otherwise she would have to ask the following witness friend:

<?php
//source: https://www.dongyao.ren/
getMenu($menus_main,$parent_id=0,$sub='children',$level=1){
        $data = array();
        foreach($menus_main as $key=>$val){
            if($val['parent_id']==$parent_id){
                $val['level']=$level;
                $val[$sub]=$this->getMenu($menus_main,$val['id'],$sub,$level+1);
                $data[] = $val;
            }
        }
        return $data;
    }

Active confession

The following is the relationship map of these lists, which has been in a cold sweat

Recall the details

Now I ask you to tell me the details of your understanding with these people. The evidence is conclusive. Don't hold on
Recall the witness just now. After discussion, a train of thought was determined as follows:

<?php
//source: https://www.dongyao.ren/
function getMenu($menus_main,$pid=0,$sub='children',$level=1,$name = 'Main column'){
    $data = array();
    foreach($menus_main as $key=>$val){
        
        echo "In the second".$level."Layer 2 troubleshooting".$val['id']."No. person(Pid:".$val['pid']."): <span style='color:red'><b>".$val['name']."</b></span>Belong to[".$name."]---here data value".json_encode($data,JSON_UNESCAPED_UNICODE);
        
        if($val['pid']==$pid){
            echo "<span style='color:green'>: The evidence is conclusive. Continue the investigation".($level+1)."layer</span><hr>";
            $val['level']=$level;
            // unset($menus_main[$key]);
            $val[$sub]=getMenu($menus_main,$val['id'],$sub,$level+1,$val['name']);
            $data[] = $val;
        }else{
            if($val['id'] == $pid){
                echo "<span style='color:red'>---If the conditions are not met, start to return---</span><hr>";
            }else{
                echo "<span style='color:red'>---If the conditions are not met [not the same type], start to return---</span><hr>";
            }
            
        }
        
    }
    return $data;
}

The specific chat process is as follows:


... there's still a lot left out

Too much content, can't finish at one breath, talk about the key points

Related process

By comparing the relationship map and this chat record, you can see:

Due to the tense process, the figure drawn by shaking hands probably means such a simplified figure

  • First, check the ex girlfriend to make sure it's a big man. At this time, we are not sure whether there are other related people under this big man, so we put it in the pending area first. At this time, the $data variable has not been assigned!
  • The second round is to check whether everyone has a relationship with his ex girlfriend. After interrogation one by one, irrelevant personnel will be excluded temporarily;
  • At the beginning, I met my own situation. Skip. The first step of the character has been determined
  • The second Er Mao came into sight and determined that it was related to his ex girlfriend and met the investigation conditions. At this time, the personnel behind Er Mao should wait in place and wait until Er Mao finished checking
  • At this time, we entered the investigation of Er Mao's relationship chain, and continued to pull eight key figures over to ask if they had anything to do with ER Mao
  • When I asked Xiaohong, I found that Xiaohong was the child of Er Mao. Meeting the investigation conditions, I continued to pull eight key figures over and asked whether it had anything to do with Xiaohong
  • It is found that no one has anything to do with Xiaohong. At this time, mark the information that Xiaohong is a 20-year-old child into the $data variable

At this step, one of the relationship chains has been cleared and assigned $data. Currently, only Xiao Hong is marked and assigned. Currently, relevant people in other relationship chains are still waiting for investigation. Continue to look down:

  • Looking back, I continued to the position where the investigation was interrupted last time. I continued the investigation from Xiaohong, and then found that Xiaolv was also a child of Er Mao. The follow-up work was the same as Xiaohong.
  • It is concluded that Xiaolv has no related people. Exit the current relationship chain and add the survey results to the back of the previous step

At this time, the $data variable contains two characters, Xiao Hong and Xiao green. The follow-up work continues

  • Back to the position interrupted last time, $data currently contains the total information of Er Mao and two children. Then continue to investigate from behind Er Mao. It is found that Da Mao is also an ex girlfriend camp. The follow-up relationship chain investigation is the same as Er Mao:

After a round of investigation, it is found that Da Mao is relatively simple and has no children. End the investigation and record Da Mao's investigation results in $data. At this time, the data variable contains Da Mao, er Mao and their child relationship information.

......
The logic of follow-up other relationship chains is the same as above, repeatedly. Interrogation and investigation one by one, and if suspicious is found, it will be temporarily interrupted. Follow up, follow the suspicious personnel to investigate. After the investigation, register the investigation results, go back and continue to dig deep from the interrupted position, and so on.

Chain of evidence

The following is all the codes of this process. You can execute them yourself. See

<?php
//source: https://www.dongyao.ren/
function getMenu($menus_main,$pid=0,$sub='children',$level=1,$name = 'Main column'){
    $data = array();
    foreach($menus_main as $key=>$val){
        
        echo "In the second".$level."Layer 2 troubleshooting".$val['id']."No. person(Pid:".$val['pid']."): <span style='color:red'><b>".$val['name']."</b></span>Belong to[".$name."]---here data value".json_encode($data,JSON_UNESCAPED_UNICODE);
        
        if($val['pid']==$pid){
            echo "<span style='color:green'>: The evidence is conclusive. Continue the investigation".($level+1)."layer</span><hr>";
            $val['level']=$level;
            // unset($menus_main[$key]);
            $val[$sub]=getMenu($menus_main,$val['id'],$sub,$level+1,$val['name']);
            $data[] = $val;
        }else{
            if($val['id'] == $pid){
                echo "<span style='color:red'>---If the conditions are not met, start to return---</span><hr>";
            }else{
                echo "<span style='color:red'>---If the conditions are not met [not the same type], start to return---</span><hr>";
            }
            
        }
        
    }
    return $data;
}


$arr = [
    [
        'id' =>1,
        'pid'=>0,
        'name' => 'ex-girlfriend'
    ],
    [
        'id' =>2,
        'pid'=>1,
        'name' =>'twenty fen'
    ],
    [
        'id' =>3,
        'pid'=>0,
        'name' =>'Suspicious person'
    ],
    [
        'id' =>4,
        'pid'=>2,
        'name' =>'Xiao Hong'
    ],
    [
        'id' =>5,
        'pid'=>2,
        'name' =>'Little green'
    ],
    [
        'id' =>6,
        'pid'=>3,
        'name' =>'colleague'
    ],
    [
        'id' =>7,
        'pid'=>1,
        'name' =>'Big hair'
    ],
    [
        'id' =>8,
        'pid'=>3,
        'name' =>'classmate'
    ]
];
getMenu($arr);return;

AD

Welcome to the blog: https://www.dongyao.ren

Keywords: PHP Python Java recursion

Added by maralynnj on Wed, 22 Dec 2021 21:24:54 +0200