PHP tree-Recursive Implementation is not required, and php tree is not required
PHP tree-implementation without Recursion
/*** Create a parent node tree array * parameter * $ ar array, data organized in the list of joins * $ the subscript or associated key name of the primary key in the id array * $ the subscript or associated key name of the parent key in the pid array * returns a multi-dimensional array **/ function find_parent ($ ar, $ id = 'id', $ pid = 'pid ') {foreach ($ ar as $ v) $ t [$ v [$ id] = $ v; foreach ($ t as $ k =>$ item) {if ($ item [$ pid]) {if (! Isset ($ t [$ item [$ pid] ['parent'] [$ item [$ pid]) $ t [$ item [$ id] ['parent'] [$ item [$ pid] = & $ t [$ item [$ pid];} return $ t;}/*** create a subnode tree array * parameter * $ ar array, data organized in the list of joins * $ the subscript or associated key name of the primary key in the id array * $ the subscript or associated key name of the parent key in the pid array * returns a multi-dimensional array **/ function find_child ($ ar, $ id = 'id', $ pid = 'pid ') {foreach ($ ar as $ v) $ t [$ v [$ id] = $ v; foreach ($ t as $ k =>$ item) {if ($ item [$ pid]) {$ t [$ item [$ pid] ['child '] [$ item [$ id] = & $ t [$ k] ;}} return $ t ;} $ data = array ('id' => 1, 'parent' => 0, 'name' => 'grandpa'), array ('id' => 2, 'parent' => 1, 'name' => 'father '), array ('id' => 3, 'parent' => 1, 'name' => 'Uncle '), array ('id' => 4, 'parent' => 2, 'name' => 'your '), array ('id' => 5, 'parent' => 4, 'name' => 'son'),); $ p = find_parent ($ data, 'id ', 'parent'); $ c = find_child ($ data, 'id', 'parent'); Print_r ($ c );
Execution result:
Array ([1] => Array ([ID] => 1 [PARENT] => 0 [NAME] => grandfather [child] => Array ([2] => Array ([ID] => 2 [PARENT] => 1 [NAME] => father [child] => Array ([4] => Array ([ID] => 4 [PARENT] => 2 [NAME] => yourself [child] => Array ([5] => Array ([ID] => 5 [PARENT] => 4 [NAME] => son ))))) [3] => Array ([ID] => 3 [PARENT] => 1 [NAME] => uncle ))) [2] => Array ([ID] => 2 [PARENT] => 1 [NAME] => PARENT [child] => Array ([4] => Array ([ ID] => 4 [PARENT] => 2 [NAME] => yourself [child] => Array ([5] => Array ([ID] => 5 [PARENT] => 4 [NAME] => son ))))) [3] => Array ([ID] => 3 [PARENT] => 1 [NAME] => uncle) [4] => Array ([ID] => 4 [PARENT] => 2 [NAME] => yourself [child] => Array ([5] => Array ([ ID] => 5 [PARENT] => 4 [NAME] => son ))) [5] => Array ([ID] => 5 [PARENT] => 4 [NAME] => son ))
The above PHP tree-the implementation method without recursion is all the content shared by the editor. I hope to give you a reference and support for the help house.