<?php//analysis/* Fujian Fuzhou Longyan Guangdong Guangzhou Foshan Jinjiang Jiangmen Jiangxi Province Nanchang Ganzhou Ningdou County Dayu County Envelopes County Jian Cat_idpriparent_ ID1 Fujian 02 Fuzhou City 13 Longyan 14 Guangdong 05 Guangzhou 46 Foshan 47 Jinjiang 48 Jiangmen 79 Jiangxi 010 Nanchang 911 Ganzhou 912 Ningdou County 1113 Dayu County 1114 Envelope County 1115 Jian 916*/?><?phpheader ( "Content-type:text/html;charset=utf-8");//php implements an infinitely classified $area = array (array (' cat_id ' =>1, ' province ' = > ' Fujian Province ', ' parent_id ' =>0), array (' cat_id ' =>2, ' province ' = ' Longyan ', ' parent_id ' =>1), array (' cat_id ' =>3 , ' province ' = ' Guangdong province ', ' parent_id ' =>0, Array (' cat_id ' =>4, ' province ' = ' Foshan ', ' parent_id ' =>3), Array (' cat_id ' =>5, ' province ' = ' Jiangxi ', ' parent_id ' =>0 ', Array (' cat_id ' =>6, ' province ' = ' Nanchang ', ' parent_id ' = >5), Array (' cat_id ' =>7, ' province ' = ' Ganzhou ', ' parent_id ' =>5), array (' cat_id ' =>8, ' province ' = ' Ningdou County ', ' parent_id ' =>7),);/* Parameter description: $area:array An array, the column that holds the category $parent_id:int as a condition of the query $lev:int implement sub-column indentation */ Function sortarea ($area, $parent _id = 0, $lev = 1) {//Declare a variable to be static, this variable is an array of static $list, foreach ($area as $v) {//If parent id=0, remove topLayer columns, and then passed through recursion to cat_id, to determine whether parent_id equals Cat_idif ($v [' parent_id '] == $parent _id) {//$lev variable into $v array $v[' Lev '] = $lev; $list [] = $v; Sortarea ($area, $v [' cat_id '], $lev + 1)}} return $list;} $list = sortarea ($area, 0,1); foreach ($list as $v) {//Sub-column indent echo str_ with str_repeat function Repeat ('     ', $v [' Lev ']), $v [' Province '], ' <br /> ';}? >
This article is from the "Small City Studio" blog, please be sure to keep this source http://xcroom.blog.51cto.com/7941996/1650586
Using recursion to realize infinite class classification under PHP