Php recursive method implementation unlimited classification _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php recursive method to implement unlimited classification instances. Array: The code is as follows $ itemsarray (array (id1, pid0, name Level 11), array (id11, pid0, namewww.111cn.net level 12), array (id2, pid1, array:

The code is as follows:
$ Items = array (
Array ('id' => 1, 'pid '=> 0, 'name' => 'Level 1 11 '),
Array ('id' => 11, 'pid '=> 0, 'name' => 'Www .111cn.net level 12 '),
Array ('id' => 2, 'pid '=> 1, 'name' => 'Level 2 21 '),
Array ('id' => 10, 'pid '=> 11, 'name' => 'Level 2 22 '),
Array ('id' => 3, 'pid '=> 1, 'name' => 'second 23 '),
Array ('id' => 12, 'pid '=> 11, 'name' => 'second-level 24 '),
Array ('id' => 13, 'pid '=> 12, 'name' => 'third-level 31 '),
Array ('id' => 9, 'pid '=> 1, 'name' => 'second level 25 '),
);

Function:

The code is as follows:
Function formatTree ($ array, $ pid = 0 ){
$ Arr = array ();
$ Tem = array ();
Foreach ($ array as $ v ){
If ($ v ['pid '] ==$ pid ){
$ Tem = formatTree ($ array, $ v ['id']);
// Determine whether a subarray exists
$ Tem & $ v ['son'] = $ tem;
$ Arr [] = $ v;
}
}
Return $ arr;
}

The array must contain the hierarchical relationship between id and pid to specify the array value.

Articles you may be interested in
  • Unlimited Classification & Tree-based Forum implementation
  • Php mysql enables unlimited classification tree display of classification relationships
  • Unlimited articles
  • Unlimited classification
  • Unlimited Classification & Tree-based Forum implementation
  • Php unlimited classification code
  • Php unlimited classification [recursion]
  • Unlimited classification [pre-sorting and traversing tree algorithms]
  • PHP unlimited classification algorithm
  • Ecshop product unlimited classification code

The alias code is as follows $ items = array ('id' = 1, 'pid '= 0, 'name' = 'level 1 11'), array ('id' = 11, 'pid '= 0, 'name' = 'www .111cn.net level 12'), array ('id' = 2, 'pid '= 1 ,...

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.