This article mainly introduces the re-understanding of PHP references. It has a certain reference value. Now I share it with everyone. Friends in need can refer to it.
In daily development, We will encounter the need to construct a tree, build a tree structure through the relationship between id and pid, and then traverse the tree and other operations. There are two ways to implement it: 1. Recursion, 2. Reference
The advantages and disadvantages of these two methods are also obvious.
Recursion is easier to implement, but as the amount of counting data increases, its performance is very low.
The concept of citation itself is easy to understand and its performance is also very good, but there are still certain thresholds for using it well, and it is not easy to write.
The reason for writing this article is that I came across a very good solution in the past few days, which made me understand quotation again. Through this article, summarize your learning results. Ok, then go directly to the code.
If you can understand the following code after reading it, it means that you are really knowledgeable in quoting. You can also skip this article directly~.
function buildTreeByReference($data, $id = 'id', $pid = 'pid', $child = "children") { $tmp = []; //以id为健,$value为值的容器,可以很巧妙的判断根节点元素 $tree = []; //利用引用,对$data的数据进行操作 foreach ($data as $key => &$value) { // $tmp[$value['id']] = &$value; if (!isset($tmp[$value['pid']])) { $tree[] = &$tmp[$value['id']]; }else { $temp = &$tmp[$value['pid']]; $temp[$child][] = &$value; } unset($temp, $value); } return $tree; }
ok, let’s not talk about other things first. You can test this method with the following data.
$data= [ ["id" => 1, "pid" => 0 , "name" => 'Universe'], ["id" => 2, "pid" => 1 , "name" => 'Earth'], ["id" => 3, "pid" => 2 , "name" => 'China'], ["id" => 4, "pid" => 3 , "name" => 'Beijing'], ];
Additional note: This method needs to pay attention to the fact that the parent node needs to be in front. It is not suitable for unordered data, so if it is unordered, it must be sorted first.
If there are no accidents, the printed result should be as follows:
array(1) { [0]=> array(4) { ["id"]=> int(1) ["pid"]=> int(0) ["name"]=> string(8) "Universe" ["children"]=> array(1) { [0]=> array(4) { ["id"]=> int(2) ["pid"]=> int(1) ["name"]=> string(5) "Earth" ["children"]=> array(1) { [0]=> array(4) { ["id"]=> int(3) ["pid"]=> int(2) ["name"]=> string(5) "China" ["children"]=> array(1) { [0]=> array(3) { ["id"]=> int(4) ["pid"]=> int(3) ["name"]=> string(7) "Beijing" } } } } } } } }
If you are here, you still If you don’t understand, it doesn’t matter. Let’s analyze it one by one.
In fact, to completely understand this solution, you need to understand two parts.
foreach assignment principle
Quoted principle
foreach
$data = ["student", "teacher"]; foreach ($data as $index => $item) { }
Note that each time the loop is executed, the "values" of $data[0] and $data[1] are copied and then assigned to $item
Quote (Be sure to try it yourself)
$a = 1; $b = &$a; $c = $b; $c = 2; 猜猜看 $b = ?;
If you have any questions about the quote, click here
If you can understand the above foreach and quote, and understand All the implementation process of this solution, then congratulations, you have learned so well! But if you still have difficulties, it doesn’t matter, let’s do it step by step.
ok, Take a deep breath and follow my thoughts, let’s go step by step.
First let’s take a look at the original function
function buildTreeByReference($data, $id = 'id', $pid = 'pid', $child = "children") { $tmp = []; #以id为健,$value为值的容器,可以很巧妙的判断根节点元素 $tree = []; #利用引用,对$data的数据进行操作 foreach ($data as $key => &$value) { #&$value取到$data元素对应值的引用 $tmp[$value['id']] = &$value; #以$value['id']为键,&$value引用为值push到$tmp中, #这样可以巧妙的判断当前元素是否为根节点 if (!isset($tmp[$value['pid']])) { #将根节点push到$tree中 $tree[] = &$tmp[$value['id']]; }else { #若当前元素的父节点存在于$tmp中, 引用获取$tmp中对应父节点的值 $temp = &$tmp[$value['pid']]; #然后将当前元素push到其父节点的children中 $temp[$child][] = &$value; } #为了不引起变量污染, 引用用完后,需要unset掉 unset($temp, $value); } return $tree; }
First loop
function buildTreeByReference($data, $id = 'id', $pid = 'pid', $child = "children") { # $tmp = []; # $tree = []; # foreach ($data as $key => &$value) { // $tmp[$value['id']] = &$value; if (!isset($tmp[$value['pid']])) { $tree[] = &$tmp[$value['id']]; }else { # $temp = &$tmp[$value['pid']]; # $temp[$child][] = &$value; # } unset($temp, $value); } return $tree; }
Variable situation:
$data[0] = ["id" => 1, "pid" = > 0 , "name" => 'Universe'];
$tmp[1] = &$data[0];
$tree[] = &$data[0]
Second loop
function buildTreeByReference($data, $id = 'id', $pid = 'pid', $child = "children") { # $tmp = []; # $tree = []; # foreach ($data as $key => &$value) { // $tmp[$value['id']] = &$value; # if (!isset($tmp[$value['pid']])) { # $tree[] = &$tmp[$value['id']]; }else { $temp = &$tmp[$value['pid']]; $temp[$child][] = &$value; } unset($temp, $value); } return $tree; }
Variable situation:
$data[1] = ["id" => 2 , "pid" => 1 , "name" => 'Earth'];
$value=&$data[1];
$tmp[2] = &$data[1];
Note:
$temp is &$tmp[1], which points to the same address as $data[0]
So $temp['children'][] = &$ value , the result of the operation is:
$data[ [ "id" => 1, "pid" => 0 , "name" => 'Universe' "children"=>[ &$data[1], //注意:存储的是引用 ] ] ... ]
4. The third loop
function buildTreeByReference($data, $id = 'id', $pid = 'pid', $child = "children") { # $tmp = []; # $tree = []; # foreach ($data as $key => &$value) { // $tmp[$value['id']] = &$value; # if (!isset($tmp[$value['pid']])) { # $tree[] = &$tmp[$value['id']]; }else { $temp = &$tmp[$value['pid']]; $temp[$child][] = &$value; } unset($temp, $value); } return $tree; }
variable situation:
$data[2] = ["id" = > 3, "pid" => 2 , "name" => 'China'];
$value = &$data[2];
$tmp[3] = &$data[2 ];
Note:
$temp is &$tmp[2], which points to the same address as $data[1]
So $temp['children' ][] = &$value , the result of the operation is:
Note here:
This is the reference to $data[1] stored in children during the second cycle
$data[ [ "id" => 1, "pid" => 0 , "name" => 'Universe' "children"=>[ &$data[1], //注意:存储的是引用 ] ] ... ]
In the third loop, it is $data[1]['children'][] = &$value, and $value points to $data[2]
, so the result is:
$data[ [ "id" => 1, "pid" => 0 , "name" => 'Universe' "children"=>[ // &$data[1], //注意:存储的是引用 [ "id" => 2, "pid" => 1 , "name" => 'Earth' "children" => [ &data[2] //注意:存储的是引用 ] ] ] ] ] ... ]
5. The fourth cycle
function buildTreeByReference($data, $id = 'id', $pid = 'pid', $child = "children") { # $tmp = []; # $tree = []; # foreach ($data as $key => &$value) { // $tmp[$value['id']] = &$value; # if (!isset($tmp[$value['pid']])) { # $tree[] = &$tmp[$value['id']]; }else { $temp = &$tmp[$value['pid']]; $temp[$child][] = &$value; } unset($temp, $value); } return $tree; }
Variable situation:
$data[3] = ["id" => 4, "pid" => 3 , "name" => 'Beijing'];
$value = &$data[3];
$tmp[3] = &$data[3];
Note:
$temp is &$tmp[2], which points to the same address as $data[1]
So $temp['children'][] = &$value, the result of the operation Yes:
Note here:
This is the reference to $data[2] stored in children during the third cycle.
$data[ [ "id" => 1, "pid" => 0 , "name" => 'Universe' "children"=>[ // &$data[1], //注意:存储的是引用 [ "id" => 2, "pid" => 1 , "name" => 'Earth' "children" => [ &data[2] //注意:存储的是引用 ] ] ] ] ] ... ]
During the fourth cycle, it is $ data[2]['children'][] = &$value, and $value points to $data[3]
, so the result is:
$data[ [ "id" => 1, "pid" => 0 , "name" => 'Universe' "children"=>[ // &$data[1], //注意:存储的是引用 [ "id" => 2, "pid" => 1 , "name" => 'Earth' "children" => [ // &data[2] //注意:存储的是引用 [ "id" => 3, "pid" => 2 , "name" => 'China' "children" =>[ &$data[3]; //注意:存储的是引用 ] ] ] ] ] ] ] ... ]
ok. At this point, the entire execution process goes Got it, do you understand? :)
By the way, there is another method, which is also through reference. I will not analyze this. If you understand the above method, the following one is relatively simple.
public static function buildTreeByReference1($data, $id = 'id', $pid = 'pid', $child = "children") { $tmp = []; foreach ($data as $key => $value) { $tmp[$value[$id]] = $value; } $tree = []; foreach ($tmp as $key => $value) { if (isset($tmp[$value['pid']])) { $tmp[$value['pid']]['children'][] = &$tmp[$key]; }else{ $tree[] = &$tmp[$key]; } } return $tree; }
Related recommendations:
A summary of several usage examples cited by php
The above is the detailed content of Re-understanding PHP references. For more information, please follow other related articles on the PHP Chinese website!