PHP unlimited classification Spanning Tree instance code

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff. The above tree generation method can also be simplified to five lines: code example :? PhpfunctiongenerateTree ($ items) {foreach ($ itemsas $ item) $ items [$ item [pid] [son] [$ item [id] $ items [$ item [id]; returnisset

Welcome to the Linux community forum and interact with 2 million technical staff> go to the above tree generation method and streamline it to five lines: Sample Code :? Php function generateTree ($ items) {foreach ($ items as $ item) $ items [$ item ['pid '] ['son'] [$ item ['id'] = $ items [$ item ['id']; return isset

Welcome to the Linux community forum and interact with 2 million technicians>

The preceding tree generation method can also be reduced to five rows:

Sample Code:

  

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.

It seems that the actual use of this Code is not obvious. to retrieve the formatted tree data, we still need to recursion:

Sample Code:

/**

* How to retrieve formatted tree data

* @ Site <

*/

$ Tree = generateTree ($ items );

Function getTreeData ($ tree ){

Foreach ($ tree as $ t ){

Echo $ t ['name'].'
';

If (isset ($ t ['son']) {

GetTreeData ($ t ['son']);

}

}

}

GetTreeData ($ tree );

[1] [2]

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.