<?phpheader ("Content-type:text/html;charset=utf-8");/* Infinite class classification. 2 applications involved 0-Find the sub-column of the designated column 1 is-find the descendants of the designated column, that is, descendants of the tree 2 is-find the parent column of the designated column/Parent column .... Top column, the family tree */$aarr = array (Array (' ID ' =>1, ' name ' = ' Anhui ', ' pid ' =>0), array (' ID ' =>2, ' name ' = = ') Haidian ', ' pid ' =>7), array (' ID ' =>3, ' name ' = ' raccoon County ', ' pid ' =>5), array (' ID ' =>4, ' name ' = ' changping ', ' pid ' =>7), Array (' ID ' =>5, ' name ' = ' Huaibei ', ' pid ' =>1), array (' ID ' =>6, ' name ' = ' Chaoyang ', ' pid ' =>7), array (' ID ' =>7, ' Name ' = ' + ' Beijing ', ' pid ' =>0), array (' ID ' =>8, ' name ' = ' on ', ' pid ' =>2)); Function subtree ($arr, $id =0,$ lev=1) {$subs = array (); //descendant tree foreach ($arr as $v) {if ($v [' pid '] == $id) {$v [' Lev '] = $lev; $subs [] = $v; $subs = array_merge ($subs, subtree ($arr, $v [' id '], $lev +1 );//array_merge let the array stick up}}return $subs;} $tree = subtree ($aarr, 0,1), foreach ($tree as $v) {echo str_repeat (' ') , $v [' Lev ']). $v [' name ']. ' <br/> ';//str_repeat replacement}
The previous lesson was to use the static method here is not practical static, but using Array_merge to achieve the effect
This article is from the "Kill Thunder Dog Blog" blog, please be sure to keep this source http://leigood.blog.51cto.com/10796867/1705828
Life Lei Dog---infinite pole classification NO3