PHP recursive tree structure function question?

function getTree ($data, $pId)
{

 $tree = "";   // -------------------1

foreach ($data as $k = > $v)
{
if ($v ["cate_ParentId"] = = $pId)
{/ / Father finds son

$v["cate_ParentId"] = getTree($data, $v["cate_Id"]); //---------------------2

 $tree[] = $v;     //------------------------3
 
 //unset($data[$k]);
}

}

 return $tree; // -----------------4

}
$tree = getTree ($data, 0);

question:
1. This is the recursive method I saw on the Internet. I now have a question: define $tree =""in the first step, which means that every time the function getTree () is called, it will be re-assigned to $tree, so why is it that in the end, return $tree is still a complete tree structure, and there is no lack of data?

2. For example, a node: oneself-> son-> grandson. According to the above function execution process, [oneself-> son] this process, the function always runs between steps 1 and 2. Wait until the [grandson] node is already the last node, so step 4 will be executed, so shouldn"t the data in $tree be empty?

Please explain to me that I am very dizzy now.

Apr.05,2021

this tree is not that tree

the tree points to a different

each time it recurses.
Menu