PHP iteration and Recursion implement unlimited classification, and php recursion is unlimited

Source: Internet
Author: User

PHP iteration and Recursion implement unlimited classification, and php recursion is unlimited

Infinitus classification is a common situation in development. Therefore, this article summarizes and summarizes the common infinitus classification algorithms.

1. Implement loop iteration

$ Arr = [1 => ['id' => 1, 'name' => 'parent 1', 'Father '=> NULL], 2 => ['id' => 2, 'name' => 'parent 2', 'Father '=> NULL], 3 => ['id' => 3, 'name' => 'parent 3', 'Father '=> NULL], 4 => ['id' => 4, 'name' => 'children 1-1 ', 'Father '=> 1], 5 => ['id' => 5, 'name' => 'children 1-2', 'Father '=> 1], 6 => ['id' => 6, 'name' => 'children 1-3 ', 'Father' => 1], 7 => ['id' => 7, 'name' => 'er 2-1 ', 'Father' => 2], 8 => ['id' => 8, 'name' => 'er 2-1 ', 'Father '=> 2], 9 => ['id' => 9, 'name' => 'er 3-1', 'Father' => 3], 10 => ['id' => 10, 'name' => 'children 3-1-1 ', 'Father' => 9], 11 => ['id' => 11, 'name' => 'children 1-1-1 ', 'Father' => 4], 12 => ['id' => 12, 'name' => 'children 2-1-1 ', 'fatsher' => 7],]; function generateTree ($ items) {$ tree = array (); foreach ($ items as $ item) {if (isset ($ items [$ item ['father ']) {$ items [$ item ['father '] ['son'] [] = & $ items [$ item ['id'];} else {$ tree [] = & $ items [$ item ['id'] ;}} return $ tree ;}$ tree = generateTree ($ arr ); print_r (json_encode ($ tree ));

Output:

Analysis:

This algorithm uses loop iteration to output linear structures in a tree structure according to the parent-child relationship. The key of the algorithm is to use references.

Advantage: fast and efficient.

Disadvantage: the key value of the array must be the same as the id value, so it is not easy to retrieve data (also use iteration to obtain data)

2. Recursive Implementation

$ Arr = [0 => ['id' => 1, 'name' => 'parent 1', 'Father '=> 0], 1 => ['id' => 2, 'name' => 'parent 2', 'Father '=> 0], 2 => ['id' => 3, 'name' => 'parent 3', 'Father '=> 0], 3 => ['id' => 4, 'name' => 'children 1-1 ', 'Father '=> 1], 4 => ['id' => 5, 'name' => 'children 1-2', 'Father '=> 1], 5 => ['id' => 6, 'name' => 'children 1-3 ', 'Father' => 1], 6 => ['id' => 7, 'name' => 'er 2-1 ', 'Father' => 2], 7 => ['id' => 8, 'name' => 'er 2-1 ', 'Father '=> 2], 8 => ['id' => 9, 'name' => 'er 3-1', 'Father' => 3], 9 => ['id' => 10, 'name' => 'children 3-1-1 ', 'Father' => 9], 10 => ['id' => 11, 'name' => 'children 1-1-1 ', 'Father' => 4], 11 => ['id' => 12, 'name' => 'children 2-1-1 ', 'fatsher' => 7],]; function generateTree ($ arr, $ id, $ step) {static $ tree = []; foreach ($ arr as $ key => $ val) {if ($ val ['father '] = $ id) {$ flg = str_repeat ('employee ―', $ step); $ val ['name'] = $ flg. $ val ['name']; $ tree [] = $ val; generateTree ($ arr, $ val ['id'], $ step + 1 );}} return $ tree;} $ tree = generateTree ($ arr, 0, 0); foreach ($ tree as $ val) {echo $ val ['name']. '<br> ';}

Output:

Analysis:

Recursion is used. The key value and id value of the array can be different. Finally, the array is output in order.

Advantage: convenient traversal and searching for parent and child elements

Disadvantage: php is not good at recursion, and the efficiency will be significantly reduced when the data volume is large.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.