Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to reference spanning tree structure in PHP

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

PHP how to reference spanning tree structure, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

A reference in PHP means that two variables point to the same place. As long as the & symbol is added to the variable, it becomes a reference.

*

The output is as follows:

A: (refcount=3, is_ref=1) = 'aaa'b: (refcount=3, is_ref=1) =' aaa'c: (refcount=3, is_ref=1) = 'aaa'

"aaa" has three references, and is_ref is the reference type, which means that whether I change $b or $c, the "aaa" will be changed.

According to the above principle, the row-by-line data with pid stored in a database can be transformed into a multi-level tree structure.

$data=array ("id" = > 2, "pid" = > 1), array ("id" = > 3, "pid" = > 1), array ("id" = > 4, "pid" = > 2), array ("id" = > 5, "pid" = > 2), array ("id" = > 6, "pid" = > 3), array ("id" = > 7, "pid" = > 3), array ("id" = > 1, "pid" = > 0),); $refer=array () / / storing the reference relationship between the primary key and the array unit / / traversing foreach ($data as $k = > $v) {$refer [$v ['id']] = & $data [$k]; / / establishing a correspondence for each array member} / / traversing 2foreach ($data as $k = > $v) {$parent=&$refer [$v [' pid']]; / getting a reference to the parent category $parent ['child'] [] = & $data [$k] / / add another reference member} print_r ($data) to the children of the parent category

Using a $refer array, the time complexity is O (n), only a single-layer loop is needed, and the original $data data is modified directly by reference to generate a tree structure.

Array ([0] = > Array ([id] = > 2 [pid] = > 1 [child] = > Array ([0] = > Array ([id] = > 4 [pid]) = > 2) [1] = > Array ([id] = > 5 [pid] = > 2) [1] = > Array ( [id] = > 3 [pid] = > 1 [child] = > Array ([0] = > Array ([id] = > 6 [pid] = > 3) ) [1] = > Array ([id] = > 7 [pid] = > 3) [2] = > Array ([id] = > 4 [pid] = > 2) [3] = > Array ([id] = > 5 [pid] = > 2) [4] = > Array ([id] = > 6 [pid] = > 3) [5] = > Array ([id] = > 7) [pid] = > 3) [6] = > Array ([id] = > 1 [pid] = > 0 [child] = > Array ([0] = > Array ([id] = > 2) [pid] = > 1 [child] = > Array ([0] = > Array ([id] = > 4) [pid] = > 2) [1] = > Array ([id] = > 5 [pid] = > 2)) [1] = > Array ([id] = > 3 [pid] = > 1 [child] = > Array ([0] = > Array ( [id] = > 6 [pid] = > 3) [1] = > Array ( [id] = > 7 [pid] = > 3) $aqaaa' Is it helpful for you to read the above content after reading the above? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report