Share a awesome PHP Infinitus classification tree generation method and use it to reference

Source: Internet
Author: User
Are you still using recursive traversal of Infinitus categories that wastes time and memory? after reading this article, I think you should change it. This is a very simple PHP Infinitus classification spanning tree method I saw on OSChina. it happened to be referenced and shared. Function

Are you still using recursive traversal of Infinitus categories that wastes time and memory? after reading this article, I think you should change it.
This is a very simple PHP Infinitus classification spanning tree method I saw on OSChina. it happened to be referenced and shared.

Function generateTree ($ items) {$ tree = array (); foreach ($ items as $ item) {if (isset ($ items [$ item ['pid']) {$ items [$ item ['pid '] ['son'] [] = & $ items [$ item ['id'];} else {$ tree [] = & $ items [$ item ['id'] ;}} return $ tree ;} $ items = array (1 => array ('id' => 1, 'pid '=> 0, 'name' => 'Anhui province '), 2 => array ('id' => 2, 'pid '=> 0, 'name' => 'Zhejiang province '), 3 => array ('id' => 3, 'pid '=> 1, 'name' => 'Hefei '), 4 => array ('id' => 4, 'pid '=> 3, 'name' => 'changfeng County '), 5 => array ('id' => 5, 'pid '=> 1, 'name' => 'anqing'),); print_r (generateTree ($ items ));

The following output is displayed:
Array ([0] => Array ([id] => 1 [pid] => 0 [name] => Anhui [son] => Array ([0] => Array ([id] => 3 [pid] => 1 [name] => Hefei [son] => Array ([0] => Array ([id] => 4 [pid] => 3 [name] => Changfeng County ))) [1] => Array ([id] => 5 [pid] => 1 [name] => Anqing ))) [1] => Array ([id] => 2 [pid] => 0 [name] => Zhejiang ))

The preceding tree generation method can also be reduced to five rows:
function generateTree($items){    foreach($items as $item)        $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']];    return isset($items[0]['son']) ? $items[0]['son'] : array();}

The above method of tree structure of Infinitus classified data is worth learning. But I don't think the actual use of this code is obvious. You still need to recursively retrieve the formatted tree data:
/*** How to retrieve formatted tree data * @ blog
 
  
*/$ Tree = generateTree ($ items); function getTreeData ($ tree) {foreach ($ tree as $ t) {echo $ t ['name'].'
  
'; If (isset ($ t ['son']) {getTreeData ($ t ['son']) ;}} getTreeData ($ tree );

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.