Recursive infinite level classification for PHP

Source: Internet
Author: User

<?php
Header ("content-type:text/html; Charset=utf-8 ");
/**
*
* @category Contry_category to achieve an infinite level classification, similar to the division of categories, commonly used in column navigation
*
*/


/**
+-------------------------------------------------------------------------------+
| ID Name Category ID |
| 1 China 0 |
| 2 Beijing 1 |
| 3 Shanghai 1 |
| 4 USA 0 |
* 5 New York 4 *
* 6 Washington 4 *
* 7 California, 4 *
+-------------------------------------------------------------------------------+
*
* Category ID 0 is the top column
* Category ID for the same ID is the category
*/


Class contry_category{
public $i =-1;
Public Function Index ($array) {
$this->i++;
if ($array [$this->i][' category_id ']==0) {
echo "├". $array [$this->i]["name"]. " <br/> ";
for ($j =0; $j < count ($array); $j + +) {
if ($array [$j] [' category_id ']== $array [$this->i][' id ']} {//view category
echo "├─┴". $array [$j] [name]. " <br/> ";
}
}
}
foreach ($array as $v) {
if ($v [' category_id ']==0)
$ary []= $v [' category_id ']; Get several top-level columns to determine recursion several times
}

if ($this->i<=count ($ary))
$this->index ($array); Recursive
}
}
$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 ' = "USA", "category_id "=>0", Array ("id" =>5, ' name ' = = "New York", "category_id" =>4), Array ("id" =>6, ' name ' = "Washington", "category_id" =>4), Array ("id" =>7, ' name ' = ' "California forint", "category_id" =>4));

$obj =new contry_category ();
$obj->index ($infos);






/**
* Print out the effect is like this
*├ China
├─┴ Beijing
├─┴ Shanghai
├ USA
├─┴ New York
├─┴ Washington
├─┴, California
*/

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.