A method of generating tree in reference implementation in PHP

Source: Internet
Author: User
This article mainly introduces the introduction of PHP in the implementation of tree generation method, interested in the reference of friends, I hope to help you.

Are you still wasting your time and wasting your memory recursively traversing infinite pole categories, reading this article, I think you should change.

This is what I saw on the Oschina a very streamlined PHP infinite pole classification Spanning tree method, skillfully in the reference, sorting and sharing.

The code is as follows:

function Generatetree ($items) {    $tree = array ();    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 ' = ' Anhui '), 2 = =    Array (' id ' = = 2, ' pid ') = 0, ' name ' = ' Zhejiang province ',    3 = = Array (' id ' = 3, ' pid ' = = 1, ' name ' = ' Hefei '),    4 = = Array (' ID ' = 4, ' pid ' = 3, ' name ' = = ' Changfeng County '),    5 = = Array (' id ' = = 5, ' pid ' = 1, ' name ' = ' Anqing '),;p rint _r (Generatetree ($items));

You can see the results printed below:

The code is as follows:

Array ([0] = = Array ([id] = 1 [pid] = 0 [name] = = Anhui Province                            [Son] = array ([0] = = Array (                            [ID] + 3 [PID] + 1 [name] = = Hefei City                                        [Son] = array ([0] = = Array                                            ([id] = 4                                 [PID] = 3 [name] = Changfeng County)                            )) [1] = = Array (                        [ID] + 5 [PID] + 1 [name] = = Anqing   )              )) [1] = = Array ([id] = 2 [PID] + 0 [name] (Zhejiang Province))


The spanning tree method above can also be reduced to 5 rows:

The code is as follows:

function Generatetree ($items) {    foreach ($items as $item)        $items [$item [' pid ']][' son '] [$item [' id ']] = &$ items[$item [' id ']];    return Isset ($items [0][' son '])? $items [0][' son ']: Array ();}

The above-mentioned method of tree-shaped structure of infinite pole classification data is worthy of reference. But I think the actual use of this code is not obvious ah, you want to take out the formatted tree data or recursive AH:

The code is as follows:

/** * How to fetch data formatted tree data */$tree = Generatetree ($items); function Gettreedata ($tree) {    foreach ($tree as $t) {        echo $t [' Name ']. ' <br> ';        if (Isset ($t [' son '])) {            gettreedata ($t [' son ']);}}}    Gettreedata ($tree);

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

How to implement persistence layer in PHP

How PHP Gets the file MIME type

How PHP operates a database to determine if a table exists

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.