1: The efficiency of the iteration is higher than the recursion, the code page is relatively small, so find the family tree recommended iteration
$area = array (' ID ' =>1, ' name ' = = ' Anhui ', ' parent ' =>0), array (' ID ' =>2, ' name ' = = ' Haidian ', ' parent ' =>7), Array (' ID ' =>3, ' name ' = ' Suixi County ', ' parent ' =>5), array (' ID ' =>4, ' name ' = ' changping ', ' parent ' =>7), array (' id ' = >5, ' name ' = ' Huaibei ', ' parent ' =>1), array (' ID ' =>6, ' name ' = ' Chaoyang ', ' parent ' =>7), array (' ID ' =>7, ' name ' = ' Beijing ', ' parent ' =>0), array (' ID ' =>8, ' name ' = ' on ', ' parent ' =>2), function Gettree ($arr, $id) {$tree = Array (), while ($id!=0) {foreach ($arr as $v) {if ($v [' id '] = = $id) {$tree [] = $v; Put the found sub-array into the empty tree array $id = $v [' parent ']; The while loop. Break;}}} return $tree;} Print_r (Gettree ($area, 8));
PHP Infinite Pole classification Find family tree