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 );