Php unlimited classification _ PHP Tutorial

Source: Internet
Author: User
Php unlimited classification. The seven of CI provides me with a method: [php]? Php *** this method is provided by @ Tonton * my. oschina. netu918697 * @ date2012-12-12 * functiongenTree5 ($ items) {fore CI's seven provides me with a method as follows:

[Php]

/**

* This method is provided by @ Tonton

* Http://my.oschina.net/u/918697

* @ Date 2012-12-12

*/

Function genTree5 ($ items ){

Foreach ($ items as $ item)

$ Items [$ item ['pid '] ['son'] [$ item ['id'] = & $ items [$ item ['id'];

Return isset ($ items [0] ['son'])? $ Items [0] ['son']: array ();

} Www.2cto.com

/**

* Format the data into a tree structure.

* @ Author Xuefen. Tong

* @ Param array $ items

* @ Return array

*/

Function genTree9 ($ items ){

$ Tree = array (); // formatted tree

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' => 'jiangxi '),

2 => array ('id' => 2, 'pid '=> 0, 'name' => 'heilongjiang province '),

3 => array ('id' => 3, 'pid '=> 1, 'name' => 'nanchang city '),

4 => array ('id' => 4, 'pid '=> 2, 'name' => 'Harbin '),

5 => array ('id' => 5, 'pid '=> 2, 'name' => 'jixi city '),

6 => array ('id' => 6, 'pid '=> 4, 'name' => 'xiangfang region '),

7 => array ('id' => 7, 'pid '=> 4, 'name' => 'nangang '),

8 => array ('id' => 8, 'pid '=> 6, 'name' =>' and Xinglu '),

9 => array ('id' => 9, 'pid '=> 7, 'name' => 'westbrook street '),

10 => array ('id' => 10, 'pid '=> 8, 'name' => 'Northeast forestry Emy '),

11 => array ('id' => 11, 'pid '=> 9, 'name' => 'Harbin Institute of Technology '),

12 => array ('id' => 12, 'pid '=> 8, 'name' => 'Harbin Normal University '),

13 => array ('id' => 13, 'pid '=> 1, 'name' => 'ganzhou city '),

14 => array ('id' => 14, 'pid '=> 13, 'name' => 'ganxian '),

15 => array ('id' => 15, 'pid '=> 13, 'name' => 'yudu County '),

16 => array ('id' => 16, 'pid '=> 14, 'name' => 'moudian Zhen '),

17 => array ('id' => 17, 'pid' => 14, 'name' => 'tagname '),

18 => array ('id' => 18, 'pid '=> 16, 'name' => 'yiyuan cune '),

19 => array ('id' => 19, 'pid '=> 16, 'name' => 'shangba cune '),

);

Echo"

";

Print_r (genTree5 ($ items ));

Print_r (genTree9 ($ items ));

// The latter output format. the former format is similar, but the array key values are different, but the data structure is not affected.

/*

Array

(

[0] => Array

(

[Id] => 1

[Pid] => 0

[Name] => Jiangxi

[Son] => Array

(

[0] => Array

(

[Id] => 3

[Pid] => 1

[Name] => Nanchang City

)

[1] => Array

(

[Id] => 13

[Pid] => 1

[Name] => Ganzhou City

[Son] => Array

(

[0] => Array

(

[Id] => 14

[Pid] => 13

[Name] => Ganxian County

[Son] => Array

(

[0] => Array

(

[Id] => 16

[Pid] => 14

[Name] => moudian town

[Son] => Array

(

[0] => Array

(

[Id] => 18

[Pid] => 16

[Name] => Yiyuan village

)

[1] => Array

(

[Id] => 19

[Pid] => 16

[Name] => Shangba village

)

)

)

[1] => Array

(

[Id] => 17

[Pid] => 14

[Name] => Datian Township

)

)

)

[1] => Array

(

[Id] => 15

[Pid] => 13

[Name] => Yudu County

)

)

)

)

)

[1] => Array

(

[Id] => 2

[Pid] => 0

[Name] => Heilongjiang province

[Son] => Array

(

[0] => Array

(

[Id] => 4

[Pid] => 2

[Name] => Harbin

[Son] => Array

(

[0] => Array

(

[Id] => 6

[Pid] => 4

[Name] => Xiangfang District

[Son] => Array

(

[0] => Array

(

[Id] => 8

[Pid] => 6

[Name] => hexinglu

[Son] => Array

(

[0] => Array

(

[Id] => 10

[Pid] => 8

[Name] =>

Northeast Forestry University

)

[1] => Array

(

[Id] => 12

[Pid] => 8

[Name] =>

Harbin Normal University

)

)

)

)

)

[1] => Array

(

[Id] => 7

[Pid] => 4

[Name] => Nangang District

[Son] => Array

(

[0] => Array

(

[Id] => 9

[Pid] => 7

[Name] => westbound street

[Son] => Array

(

[0] => Array

(

[Id] => 11

[Pid] => 9

[Name] =>

Harbin Institute of Technology

)

)

)

)

)

)

)

[1] => Array

(

[Id] => 5

[Pid] => 2

[Name] => Jixi city

)

)

)

)*

Through testing, we can complete the concept of unlimited classification. However, we can use our own data, but we cannot use the data in our database. errors may occur, such as the following errors:

[Php]

Array

(

[10] => Array

(

[Child] => Array

(

[14] =>

)

)

[11] =>

[12] =>

[9] => Array

(

[Child] => Array

(

[13] =>

)

)

)

I am very worried that only the array serial number is displayed and the corresponding data is not displayed, so that the test can be fully displayed:

[Php]

Function genTree5 ($ items ){

Foreach ($ items as $ item ){

$ Items [$ item ['id'] = $ item; // ADD THIS

$ Items [$ item ['pid '] ['son'] [$ item ['id'] = & $ items [$ item ['id'];

}

Return isset ($ items [0] ['son'])? $ Items [0] ['son']: array ();

}

However, this can only be used in ascending order of IDs, and an error is displayed in disordered order.

In most cases, the id is in ascending order, but once sorting is involved, it cannot be used again.

Previously, teacher Lu of CI provided an efficient method, namely:

[Php]

Id parent id classification

1 0 0, 1

2 1 0, 1, 2

3 1 0, 1, 3

4 3 0, 1, 3, 4

Select * from table order by grading asc

This structure will sort the classification by parent-child order, first sort the first 0, then sort the second 1, and so on, automatic classification, very easy to use (also note, make sure there are two groups, that is, start from 0, 1, or start from 0, otherwise the root classification will be displayed in advance ). "Classification" can also be called childid, because it is the Subid layer of the record.

[Php]

| -- 2

| ---- 2-1

| ------ 2-1-1

| -- 3

| ---- 3-1

| -- 4

| -- 1

| ---- 1-1

Directly traverse the display, because I have a depth field that records the depth, so it can be better displayed.

This method also has a short running time and runs in microseconds. In short, it takes a lot of time to sort databases, and other operations are quite pleasant.

Generally, when pointers are not cached, most tests are larger than the sorted time, most of which are CPU time, and the results are inaccurate.

PS: PHP has weakened the concept of address, pointer, and so on. that is to say, it is not a feature of PHP. although it can be used, some inexplicable errors may occur. So be honest ~ Shenma's big data algorithm does not need PHP to do anything. C ++ is much faster than PHP ....

At the same time, @ Tonton corrected his algorithm.

Why [php]? Php/*** this method is supplied by @ Tonton * http://my.oschina.net/u/918697 * @ date 2012-12-12 */function genTree5 ($ items) {fore...

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.