Unlimited classification implemented by recursion in php
Source: Internet
Author: User
Unlimited classification implemented by recursion in php Header ("Content-type: text/html; charset = utf-8 ");
/**
*
* @ Category contry_category: implements an unlimited classification. similar categories are commonly used in topic navigation.
*
*/
/**
+ ------------------------------------------------------------------------------- +
| Id name Category id |
| 1 China 0 |
| 2 Beijing 1 |
| 3 Shanghai 1 |
| 4 US 0 |
* 5 New York 4 *
* 6 Washington 4 *
* 7 Virginia 4 *
+ ------------------------------------------------------------------------------- +
*
* Top-level columns whose Category id is 0
* If the category id is the same as the id, the corresponding category is used.
*/
Class contry_category {
Public $ I =-1;
Public function index ($ array ){
$ This-> I ++;
If ($ array [$ this-> I] ['Category _ id'] = 0 ){
Echo "success". $ array [$ this-> I] ["name"]."
";
For ($ j = 0; $ j <count ($ array); $ j ++ ){
If ($ array [$ j] ['Category _ id'] = $ array [$ this-> I] ['id']) {// view the category
Echo "audio-extract". $ array [$ j] ["name"]."
";
}
}
}
Foreach ($ array as $ v ){
If ($ v ['Category _ id'] = 0)
$ Ary [] = $ v ['Category _ id']; // Obtain several top-level columns to determine the number of recursion times.
}
$ Obj = new contry_category ();
$ Obj-> index ($ infos );
/**
* The printed result is as follows:
* Shanghai China
BEIJING-Beijing
─ ── Shanghai
United States
├ ── New York
WASHINGTON-Washington
California-Virginia
*/
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.