Php two unlimited classification method examples, php two classification examples _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Examples of php two types of unlimited classification methods, php two types of classification instances. Examples of two infinite classification methods in php: examples of two types of php: 1. code for copying recursive methods: $ itemsarray (array (id1, pid0, name Level 11), array (id2, examples of pid php and php unlimited classification methods

1. recursive methods
The code is as follows:
$ Items = array (
Array ('id' => 1, 'pid '=> 0, 'name' => 'Level 1 11 '),
Array ('id' => 2, 'pid '=> 0, 'name' => 'Level 1 12 '),
Array ('id' => 3, 'pid '=> 1, 'name' => 'Level 2 21 '),
Array ('id' => 4, 'pid '=> 3, 'name' => 'third-level 31 '),
Array ('id' => 5, 'pid '=> 1, 'name' => 'Level 2 22 '),
Array ('id' => 6, 'pid '=> 3, 'name' => 'Level 3 32 '),
Array ('id' => 7, 'pid '=> 6, 'name' => 'level 4 41 '),
);
$ I = 0;
Function formatTree ($ arr, $ pid = 0 ){
$ Tree = array ();
$ Temp = array ();
Global $ I;
If ($ arr ){
Foreach ($ arr as $ k =>$ v ){
If ($ v ['pid '] ==$ pid ){//
$ Temp = formatTree ($ arr, $ v ['id']);
$ Temp & $ v ['son'] = $ temp;
$ Tree [] = $ v;
}
}
}
Return $ tree;
}
Print_r (formatTree ($ items ));

II. non-recursive method
The code is as follows:
Function genTree ($ 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' => 'Level 1 11 '),
2 => array ('id' => 2, 'pid '=> 1, 'name' => 'Level 2 21 '),
3 => array ('id' => 3, 'pid '=> 1, 'name' => 'Level 2 23 '),
4 => array ('id' => 4, 'pid '=> 9, 'name' => 'third-level 31 '),
5 => array ('id' => 5, 'pid '=> 4, 'name' => 'level 4 43 '),
6 => array ('id' => 6, 'pid '=> 9, 'name' => 'Level 3 32 '),
7 => array ('id' => 7, 'pid '=> 4, 'name' => '4's 41 '),
8 => array ('id' => 8, 'pid '=> 4, 'name' => '4 '),
9 => array ('id' => 9, 'pid '=> 1, 'name' => 'Level 2 25 '),
10 => array ('id' => 10, 'pid '=> 11, 'name' => 'Level 2 22 '),
11 => array ('id' => 11, 'pid '=> 0, 'name' => 'Level 1 12 '),
12 => array ('id' => 12, 'pid '=> 11, 'name' => 'second-level 24 '),
13 => array ('id' => 13, 'pid '=> 4, 'name' => 'level 4 44 '),
14 => array ('id' => 14, 'pid '=> 1, 'name' => 'second 26 '),
15 => array ('id' => 15, 'pid '=> 8, 'name' => 'Level 5 51 '),
16 => array ('id' => 16, 'pid '=> 8, 'name' => 'Level 5 52 '),
17 => array ('id' => 17, 'pid '=> 8, 'name' => 'five levels 53 '),
18 => array ('id' => 18, 'pid '=> 16, 'name' => 'six 64 '),
);
Print_r (genTree ($ items ));

Example 1: recursive method code: $ items = array ('id' = 1, 'pid '= 0, 'name' = 'level 1 11 '), array ('id' = 2, 'pid '=...

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.