Using recursion to realize infinite class classification under PHP

Source: Internet
Author: User

<?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 (' &nbsp&nbsp&nbsp ', $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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.