PHP Infinite Pole classification

Source: Internet
Author: User

Overview

?? In practical work, it is often used to classify the infinite poles. such as navigation tables and so on. To the Internet a search PHP infinite classification, a lot, but a lot of is a, and, write very messy, code a lot, those are not reliable, or self-ramming drum unlimited pole classification. Two types of infinite pole classifications are described below: 递归使用引用

Data Preparation
//Initialize raw data (id=> number,name=> display name,pid=> parent Catalog id,sort=> sort order)$data[1] =Array(' id '=' 1 ',' name '=' First level directory a ',' pid '=' 0 ',' sort '=' 1 ');$data[2] =Array(' id '=' 2 ',' name '=' level directory B ',' pid '=' 0 ',' sort '=' 2 ');$data[3] =Array(' id '=' 3 ',' name '=' level directory C ',' pid '=' 0 ',' sort '=' 3 ');$data[4] =Array(' id '=' 4 ',' name '=' level directory d ',' pid '=' 0 ',' sort '=' 4 ');$data[5] =Array(' id '=' 5 ',' name '=' Level two directory A-1 ',' pid '=' 1 ',' sort '=' 1 ');$data[6] =Array(' id '=' 6 ',' name '=' Level two directory A-2 ',' pid '=' 1 ',' sort '=' 2 ');$data[7] =Array(' id '=' 7 ',' name '=' Level two directory A-3 ',' pid '=' 1 ',' sort '=' 3 ');$data[8] =Array(' id '=' 8 ',' name '=' Level two directory B-1 ',' pid '=' 2 ',' sort '=' 1 ');$data[9] =Array(' id '=' 9 ',' name '=' Level two directory B-2 ',' pid '=' 2 ',' sort '=' 2 ');$data[Ten] =Array(' id '=' Ten ',' name '=' Level two directory B-3 ',' pid '=' 2 ',' sort '=' 3 ');$data[ One] =Array(' id '=' One ',' name '=' Level two directory C-1 ',' pid '=' 3 ',' sort '=' 2 ');$data[ A] =Array(' id '=' a ',' name '=' Level two directory D-1 ',' pid '=' 4 ',' sort '=' 1 ');$data[ -] =Array(' id '=' A ',' name '=' Level two directory D-2 ',' pid '=' 4 ',' sort '=' 2 ');$data[ -] =Array(' id '=' + ',' name '=' Level three directory a-2-1 ',' pid '=' 6 ',' sort '=' 1 ');$data[ the] =Array(' id '=' a ',' name '=' Level three directory a-2-2 ',' pid '=' 6 ',' sort '=' 2 ');$data[ -] =Array(' id '=' + ',' name '=' Level three directory c-1-1 ',' pid '=' One ',' sort '=' 1 ');$data[ -] =Array(' id '=' + ',' name '=' Level three directory b-2-1 ',' pid '=' 9 ',' sort '=' 2 ');

Write a function that shows the tree structure first

 function printtree($data,$level=0){    foreach($data  as $key=$value){ for($i=0;$i<=$level;$i++){Echo ' &emsp;&emsp; '; }Echo $value[' name '];Echo ' <br> ';if(!Empty($value[' Children ']) {Printtree ($value[' Children '],$level+1); }    }}
Recursive-infinite pole classification

Using recursion to get an array of infinite polar classifications

 function getnodetree(&$list,&$tree,$pid=0 ){    foreach($list  as $key=$value){if($pid==$value[' pid ']){$tree[$value[' id ']]=$value;unset($list[$key]); Getnodetree ($list,$tree[$value[' id ']][' Children '],$value[' id ']); }    }}
use reference-infinite pole classification
 function createnodetree(&$list,&$tree){    foreach($list  as $key=$node){if(isset($list[$node[' pid ']])){$list[$node[' pid ']][' Children '[] = &$list[$key]; }Else{$tree[] = &$list[$node[' id ']]; }    }}

It is really powerful, the code concise refining, without recursion, execution speed. This is I accidentally on a website to see, feel very practical to collect down, share to everybody.
The website address is: http://www.phpddt.com/php/generateTree.html

called
//递归-无限极分类调用getNodeTree($data,$tree);printTree($tree);//使用引用-无限极分类调用createNodeTree($data,$tree);printTree($tree);
Results

PHP Infinite Pole classification

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.