In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.