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.
}

If ($ this-> I <= count ($ ary ))
$ This-> index ($ array); // recursion
}
}
$ Infos = array ("id" => 1, 'name' => "China", "category_id" => 0), array ("id" => 2, 'name' => "Beijing", "category_id" => 1), array ("id" => 3, 'name' => "Shanghai ", "category_id" => 1), array ("id" => 4, 'name' => "us", "category_id" => 0 ), array ("id" => 5, 'name' => "New York", "category_id" => 4), array ("id" => 6, 'name' => "Washington", "category_id" => 4), array ("id" => 7, 'name' => "Virginia, California ", "category_id" => 4 ));

$ 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
*/

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.