- <?php
- In an infinite class, look up a family tree.
- $area = Array (
- 0=>Array (' cat_id ' =>1,' cat_name ' = 'Beijing ',' parent_id ' =>0),
- 1=>Array (' cat_id ' =>2,' cat_name ' = 'Guantao County ',' parent_id ' =>5),
- 2=>Array (' cat_id ' =>3,' cat_name ' = 'Haidian ',' parent_id ' =>1),
- 3=>Array (' cat_id ' =>4,' cat_name ' = 'Chaoyang ',' parent_id ' =>1),
- 4=>Array (' cat_id ' =>5,' cat_name ' = 'Hanhan ',' parent_id ' =>6),
- 5=>Array (' cat_id ' =>6,' cat_name ' = 'Hebei province ',' parent_id ' =>0),
- );
- function Gettree ($arr,$cat _id) {
- static $list = Array ();
- static $num = 0;
- foreach ($arr as $v) {
- echo + +$num.' <br/> ';
- if ($v [' cat_id '] = = $cat _id) {
- $num = 0;
- Gettree ($arr,$v [' parent_id ']);
- $list [] = $v;
- }
- }
- return $list;
- }
- $list = Gettree ($area, 2);
- Print_r ($list);
- ?>
- <?php
- Infinite classification, find descendants tree
- $area = Array (
- 0=>Array (' cat_id ' =>1,' cat_name ' = 'Beijing ',' parent_id ' =>0),
- 1=>Array (' cat_id ' =>2,' cat_name ' = 'Guantao County ',' parent_id ' =>5),
- 2=>Array (' cat_id ' =>3,' cat_name ' = 'Haidian ',' parent_id ' =>1),
- 3=>Array (' cat_id ' =>4,' cat_name ' = 'Chaoyang ',' parent_id ' =>1),
- 4=>Array (' cat_id ' =>5,' cat_name ' = 'Hanhan ',' parent_id ' =>6),
- 5=>Array (' cat_id ' =>6,' cat_name ' = 'Hebei province ',' parent_id ' =>0),
- );
- function Catsort ($arr,$parent _id = 0,$lev =1) {
- static $list = Array ();
- static $num = 0;
- foreach ($arr as $v) {
- echo + +$num. ' <br/> ';
- if ($v [' parent_id '] = = $parent _id) {
- $num = 0;
- $v [' lev '] = $lev;
- $list [] = $v;
- Catsort ($arr,$v [' cat_id '],$lev + 1);
- }
- }
- return $list;
- }
- $list = Catsort ($area, 0, 1);
- foreach ($list as $v) {
- echo str_repeat (',$v [' Lev ']),$v [' Cat_name '],' <br/> ';
- }
- ?>
Infinite Pole classification PHP implementation-Chazisun, family tree