PHP implements an infinitus classification method to generate a classification tree.

Source: Internet
Author: User

PHP implements an infinitus classification method to generate a classification tree.

This example describes how to generate a classification tree using infinitus classification in PHP. We will share this with you for your reference. The details are as follows:

The current classification database design is basically: Each category has an id primary key field, and a pid points to the id of the parent class, so that you can implement an unlimited classification, the retrieved data is in the following format:

$ Arr = array ("id" => 1, "pid" => 0, 'cat' => 'column 1 '), array ("id" => 2, "pid" => 0, 'cat' => 'column 2'), array ("id" => 3, "pid" => 1, 'cat' => 'column 3'), array ("id" => 4, "pid" => 2, 'cat' => 'column 4'), array ("id" => 5, "pid" => 1, 'cat' => 'column 5 '), array ("id" => 6, "pid" => 5, 'cat' => 'column 6'), array ("id" => 7, "pid" => 5, 'cat' => 'column 7'), array ("id" => 8, "pid" => 6, 'cat' => 'column 8'), array ("id" => 9, "pid" => 1, 'cat' => 'column 9 '), array ("id" => 10, "pid" => 0, 'cat' => 'column 10'), array ("id" => 11, "pid" => 10, 'cat' => 'column 11'), array ("id" => 12, "pid" => 11, 'cat' => 'column 12'), array ("id" => 13, "pid" => 2, 'cat' => 'column 13 '), array ("id" => 14, "pid" => 13, 'cat' => 'column 14 '));

Not to mention, process the Code directly:

// Function make_tree ($ arr) {$ refer = array (); $ tree = array (); foreach ($ arr as $ k => $ v) {$ refer [$ v ['id'] = & $ arr [$ k]; // create an array reference for the primary key} foreach ($ arr as $ k => $ v) {$ pid = $ v ['pid']; // obtain the parent id of the current category if ($ pid = 0) {$ tree [] = & $ arr [$ k]; // top-level topic} else {if (isset ($ refer [$ pid]) {$ refer [$ pid] ['subcat'] [] = & $ arr [$ k]; // if a parent Column exists, add it to the parent column's subcolumn array }}return $ tree ;}

Test run:

$cat = make_tree($arr);print_r($cat);

Running result:

Array ([0] => Array ([id] => 1 [pid] => 0 [cat] => Topic 1 [subcat] => Array ([0] => array ([id] => 3 [pid] => 1 [cat] => column 3) [1] => Array ([id] => 5 [pid] => 1 [cat] => Topic 5 [subcat] => Array ([0] => Array ([id] => 6 [pid] => 5 [cat] => column 6 [subcat] => Array ([0] => Array ([id] => 8 [pid] => 6 [cat] => Column 8 ))) [1] => Array ([id] => 7 [pid] => 5 [cat] => column 7 ))) [2] => Array ([id] => 9 [pid] => 1 [cat] => Topic 9 ))) [1] => Array ([id] => 2 [pid] => 0 [cat] => Topic 2 [subcat] => Array ([0] => Array ([id] => 4 [pid] => 2 [cat] => Topic 4) [1] => Array ([id] => 13 [pid] => 2 [cat] => column 13 [subcat] => Array ([0] => Array ([id] => 14 [pid] => 13 [cat] => 14 ))))) [2] => Array ([id] => 10 [pid] => 0 [cat] => Column 10 [subcat] => Array ([0] => Array ([id] => 11 [pid] => 10 [cat] => column 11 [subcat] => Array ([0] => Array ([id] => 12 [pid] => 11 [cat] => column 12 ))))))

If you need such an assembly format or the format to facilitate subsequent processing, you can try this method.

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.