How to generate a tree with an infinitely great number of PHP classes _ PHP Tutorial

Source: Internet
Author: User
The PHP super powerful Infinitus classification generation tree method. This article mainly introduces the generation tree method of PHP super-awesome Infinitus classification. This article uses the reference in PHP to implement the generation method of the tree, higher than the recursive method, PHP is more powerful than the Infinitus classification tree generation method.

This article mainly introduces the PHP super-powerful Infinitus classification generation tree method. This article uses the reference in PHP to implement the tree generation method, which is much higher than the recursive method. For more information, see

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.

The code is as follows:

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:

The code is as follows:

Array

(

[0] => Array

(

[Id] => 1

[Pid] => 0

[Name] => Anhui province

[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 City

)

)

)

[1] => Array

(

[Id] => 2

[Pid] => 0

[Name] => Zhejiang province

)

)

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

The code is as follows:

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:

The code is as follows:

/**

* How to retrieve formatted tree data

*/

$ Tree = generateTree ($ items );

Function getTreeData ($ tree ){

Foreach ($ tree as $ t ){

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

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

GetTreeData ($ t ['son']);

}

}

}

GetTreeData ($ tree );

Ghost This article mainly introduces the PHP super awesome Infinitus classification generation tree method. This article uses the reference in PHP to implement the tree generation method, which is higher than the recursive 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.