This article mainly introduces the implementation method of the PHP Infinitus classification function, and analyzes in detail the specific ideas, implementation code, and related precautions for php to implement Infinitus classification based on the instance form, for more information about how to implement the Infinitus classification function of PHP, see the next article. This article analyzes in detail the specific ideas, implementation code, and precautions for implementing the Infinitus classification function of php based on the instance form, for more information, see
This article describes how to implement the PHP Infinitus classification function. We will share this with you for your reference. The details are as follows:
When developing programs, we sometimes need to obtain all the columns (including first-level columns, second-level columns, and third-level columns) and form a tree structure based on the parent-child relationship. You can use recursion or reference (php references pointers similar to C or C ++, but it is just a different argument ).
Use the reference method to implement Infinitus classification
Ideas:
1. all the data to be processed is packaged into an array with the subscript primary key id (pk), so that the corresponding parent column can be obtained by The pid.
2. cyclically wrap the data. if it is the root node, add the reference to the tree. otherwise, add the reference to the child element of its parent class. In this way, although only the root node is added to the tree, if each root node has a child element, it contains a reference to the child element. Therefore, tree structures can be formed.
The code is as follows:
/*** Convert the returned dataset to Tree * @ param array $ list the dataset to be converted * @ param string $ pk auto-increment field (column id) * @ param string $ pid parent flag field * @ return array * @ author dqs <1696232133@qq.com> */function make_tree ($ list, $ pk = 'id ', $ pid = 'pid ', $ child =' _ child ', $ root = 0) {$ tree = array (); $ packData = array (); foreach ($ list as $ data) {$ packData [$ data [$ pk] = $ data;} foreach ($ packData as $ key => $ val) {if ($ val [$ pid] ==$ root) {// representing the node $ tree [] = & $ packData [$ key];} else {// find its parent class $ packData [$ val [$ pid] [$ child] [] = & $ packData [$ key] ;}} return $ tree ;}
The data structure is as follows:
Array ([0] => Array ([id] => 6 [pid] => 0 [title] => Test 1 [url] => test1 [sort] => 0 [create_time] => 1464076199 [update_time] => 1464076199 [status] => 1 [target] => 0) [1] => Array ([id] => 7 [pid] => 0 [title] => test 2 [url] => test2 [sort] => 0 [create_time] => 1464076218 [update_time] => 1464076218 [status] => 1 [target] => 0) [2] => Array ([id] => 1 [pid] => 0 [title] => homepage [url] => Index/I Ndex [sort] => 1 [create_time] => 1379475111 [update_time] => 1379923177 [status] => 1 [target] => 0) [3] => Array ([id] => 2 [pid] => 0 [title] => blog [url] => Article/index? Category = blog [sort] => 2 [create_time] => 1379475131 [update_time] => 1379483713 [status] => 1 [target] => 0 [_ child] => Array ([0] => Array ([id] => 4 [pid] => 2 [title] => Personal blog [url] => ownblog [sort] => 0 [create_time] => 1464075558 [update_time] => 1464075558 [status] => 1 [target] => 0 [_ child] => Array ([0] => Array ([id] => 8 [pid] => 4 [title] => Sina blog [url] => sinaBlog [sort] => 0 [create_time] => 1464077276 [update_time] => 1464077276 [status] => 1 [target] => 0 ))) [1] => Array ([id] => 5 [pid] => 2 [title] => others' blog [url] => otherBlog [sort] => 0 [create_time] => 1464075582 [update_time] => 1464075582 [status] => 1 [target] => 0 ))) [4] => Array ([id] => 3 [pid] => 0 [title] => official website [url] => http://www.onethink.cn [Sort] => 3 [create_time] => 1379475154 [update_time] => 1387163458 [status] => 1 [target] => 0 ))
Append the Infinitus classification function in OneThink.
/*** Convert the returned dataset to Tree * @ param array $ list the dataset to be converted * @ param string $ pid parent tag field * @ param string $ level tag field *@ return array * @ author Madang Miao er
*/Function list_to_tree ($ list, $ pk = 'id', $ pid = 'pid', $ child = '_ child', $ root = 0) {// create Tree $ tree = array (); if (is_array ($ list) {// create a primary key-based array reference $ refer = array (); foreach ($ list as $ key => $ data) {$ refer [$ data [$ pk] = & $ list [$ key];} foreach ($ list as $ key => $ data) {// Determine whether parent $ parentId exists = $ data [$ pid]; if ($ root = $ parentId) {$ tree [] = & $ list [$ key];} else {if (isset ($ refer [$ parentId]) {$ parent = & $ refer [$ parentId]; $ parent [$ child] [] = & $ list [$ key] ;}}} return $ tree ;}
Implement Infinitus classification using recursion
Ideas:
1. use a loop to obtain all the root nodes.
2. when obtaining each node, remove the node from the original data and recursively retrieve all its subnodes. the original data remains empty.
The code is implemented as follows:
Function make_tree1 ($ list, $ pk = 'id', $ pid = 'pid', $ child = '_ child', $ root = 0) {$ tree = array (); foreach ($ list as $ key => $ val) {if ($ val [$ pid] = $ root) {// obtain all the current $ pid subclasses unset ($ list [$ key]); if (! Empty ($ list) {$ child = make_tree1 ($ list, $ pk, $ pid, $ child, $ val [$ pk]); if (! Empty ($ child) {$ val ['_ child'] = $ child ;}}$ tree [] = $ val ;}} return $ tree ;}
The effect is as follows:
The above is a detailed description of the implementation method of the PHP Infinitus classification function. For more information, see other related articles in the first PHP community!