_php instance of PHP two infinite classification methods

Source: Internet
Author: User

I. Recursive method

Copy Code code as follows:

$items = Array (
Array (' ID ' =>1, ' pid ' =>0, ' name ' => ' Level 11 '),
Array (' ID ' =>2, ' pid ' =>0, ' name ' => ' Level 12 '),
Array (' ID ' =>3, ' pid ' =>1, ' name ' => ' two level 21 '),
Array (' ID ' =>4, ' pid ' =>3, ' name ' => ' three level 31 '),
Array (' ID ' =>5, ' pid ' =>1, ' name ' => ' two level 22 '),
Array (' ID ' =>6, ' pid ' =>3, ' name ' => ' three level 32 '),
Array (' ID ' =>7, ' pid ' =>6, ' name ' => ' four level 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));

second, non-recursive method
Copy Code code as follows:

function Gentree ($items) {
$tree = Array (); Well-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 11 '),
2 => Array (' ID ' => 2, ' pid ' => 1, ' name ' => ' two level 21 '),
3 => Array (' ID ' => 3, ' pid ' => 1, ' name ' => ' two level 23 '),
4 => Array (' ID ' => 4, ' pid ' => 9, ' name ' => ' three level 31 '),
5 => Array (' ID ' => 5, ' pid ' => 4, ' name ' => ' four level 43 '),
6 => Array (' ID ' => 6, ' PID ' => 9, ' name ' => ' three level 32 '),
7 => Array (' ID ' => 7, ' PID ' => 4, ' name ' => ' four level 41 '),
8 => Array (' ID ' => 8, ' PID ' => 4, ' name ' => ' four Level 42 '),
9 => Array (' ID ' => 9, ' pid ' => 1, ' name ' => ' two level 25 '),
=> Array (' ID ' =>, ' pid ' =>, ' name ' => ' two level 22 '),
One => array (' ID ' =>, ' pid ' => 0, ' name ' => ' Level 12 '),
=> Array (' ID ' =>, ' pid ' =>, ' name ' => ' two level 24 '),
=> Array (' ID ' =>, ' pid ' => 4, ' name ' => ' level four 44 '),
=> Array (' ID ' =>, ' pid ' => 1, ' name ' => ' Level two 26 '),
=> Array (' ID ' =>, ' pid ' => 8, ' name ' => ' level five ' 51 '),
=> Array (' ID ' =>, ' pid ' => 8, ' name ' => ' level five 52 '),
=> Array (' ID ' =>, ' pid ' => 8, ' name ' => ' level five 53 '),
=> Array (' ID ' =>, ' pid ' =>, ' name ' => ' six level 64 '),
);
Print_r (Gentree ($items));

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.