PHP tree generation method

Source: Internet
Author: User
This article mainly introduces the method of generating the PHP tree. The example analyzes the related skills of the php tree and node operations, which has some reference value, for more information about how to generate a tree in PHP, see the example in this article. Share it with you for your reference. The details are as follows:

This class is not written by me. only the getAll () function is added.
Php generates a tree for product classification
I don't know whether the traversal write is optimized. if you have any, please share it -.-!

Shows the running effect:

The implementation code is as follows:

<? Phpclass Tree {public $ data = array (); public $ cateArray = array (); public $ res = array (); function Tree () {} function setNode ($ id, $ parent, $ value) {$ parent = $ parent? $ Parent: 0; $ this-> data [$ id] = $ value; // print_r ($ this-> data); // echo "\ r \ n "; $ this-> cateArray [$ id] = $ parent; // Node array // print_r ($ this-> cateArray);} function getChildsTree ($ id = 0) {$ childs = array (); foreach ($ this-> cateArray as $ child => $ parent) {if ($ parent = $ id) {$ childs [$ child] = $ this-> getChildsTree ($ child) ;}} print_r ($ childs ). "/r/n"; return $ childs;} function getParentsTree ($ id = 0) {$ parents = Rray (); foreach ($ this-> cateArray as $ child => $ parent) {if ($ child = $ id) {$ parents [$ parent] = $ this-> getParentsTree ($ parent) ;}} print_r ($ parents ). "/r/n"; return $ parents;} function getChilds ($ id = 0) {$ childArray = array (); $ childs = $ this-> getChild ($ id); foreach ($ childs as $ child) {$ childArray [] = $ child; $ childArray = array_merge ($ childArray, $ this-> getChilds ($ child);} return $ childArray;} functio N getChild ($ id) {$ childs = array (); foreach ($ this-> cateArray as $ child => $ parent) {if ($ parent = $ id) {$ childs [$ child] = $ child;} return $ childs;} function getParents ($ id) {$ parentArray = array (); $ parents = $ this-> getParent ($ id); foreach ($ parents as $ parent) {$ parentArray [] = $ parent; $ parentArray = array_merge ($ parentArray, $ this-> getParents ($ parent);} return $ parentArray;} function getParent ($ id) {$ Parents = array (); foreach ($ this-> cateArray as $ child => $ parent) {if ($ child = $ id) {$ parents [$ parent] = $ parent;} return $ parents;} // Obtain the function getNodeLever ($ id) of the parent node from a single line {$ parents = array (); if (key_exists ($ this-> cateArray [$ id], $ this-> cateArray) {$ parents [] = $ this-> cateArray [$ id]; $ parents = array_merge ($ parents, $ this-> getNodeLever ($ this-> cateArray [$ id]);} return $ parents;} function getLaye R ($ id, $ preStr = '|-') {return str_repeat ($ preStr, count ($ this-> getNodeLever ($ id )));} function getValue ($ id) {return $ this-> data [$ id];} // end func // Obtain all node data to generate the TREE function getAll ($ id = 0, $ str = "|-") {if ($ id! = 0) {$ str = $ str. "|-";} // traverse all arrays to check whether the parent has an id foreach ($ this-> cateArray as $ child => $ parent) {// Check whether there are equal IDs if ($ parent = $ ID) {$ this-> res [$ child] = $ str. $ this-> getValue ($ child); $ this-> getAll ($ child, $ str);} // $ this-> res [$ child] = $ child. $ str. $ this-> getValue ($ child) ;}}// constructor // setNode ($ id, $ parent, $ value) $ tree = new Tree (); $ tree-> setNode ("1", "", ""); $ tree-> setNode ("2", "", "clothing "); $ tree-> setNode ("3", "1", "computer"); $ tree-> setNode ("4", "1", "household appliances "); $ tree-> setNode ("5", "2", "men's wear"); $ tree-> setNode ("6", "2", "women's wear "); $ tree-> setNode ("7", "3", "Notebook"); $ tree-> setNode ("8", "3", "desktop "); $ tree-> setNode ("9", "7", "HP"); $ tree-> setNode ("10", "7", "Dell "); $ tree-> setNode ("11", "8", "Martian"); $ tree-> setNode ("12", "5", "suit "); $ tree-> setNode ("13", "6", "coat"); $ tree-> setNode ("14", "9", "HP-N90 "); $ tree-> setNode ("15", "9", "HP-N91"); $ tree-> setNode ("16", "10 ", "Dell A11"); $ tree-> setNode ("17", "10", "Dell A12"); $ tree-> setNode ("18", "10 ", "Dell A13"); $ tree-> setNode ("19", "6", "pants"); $ tree-> setNode ("20", "13 ", "Long Sleeve"); $ tree-> setNode ("21", "13", "Short Sleeve"); $ tree-> setNode ("22", "20 ", "NIKE long sleeve"); $ tree-> setNode ("23", "20", "361 long sleeve"); $ tree-> setNode ("24", "22 ", "NIKE long sleeve-one size"); $ tree-> setNode ("25", "22", "NIKE long sleeve-short code "); $ tree-> setNode ("26", "14", "HP-N90-14"); $ tree-> setNode ("27", "14 ", "HP-N90-15 Inch"); $ tree-> setNode ("28", "14", "HP-N90-17 Inch"); $ tree-> setNode ("29 ", "28", "HP-N90-17 inch-high performance enterprise edition"); $ tree-> setNode ("30", "28 ", "HP-N90-17 inch-regular home"); // get all parent nodes of a node // print_r ($ tree-> getNodeLever (12 )); // print_r ($ tree-> getParentsTree (12); // $ childs = $ tree-> getChildsTree (1); // print_r ($ childs ); // echo "/r/n";/* foreach ($ childs as $ key => $ value) {echo $ key."
"; // Echo $ tree-> getLayer ($ key). $ tree-> getValue ($ key )."
";} */$ Tree-> getAll (); foreach ($ tree-> res as $ val) {echo $ val ."
";}?> Generate TreeGenerate tag test <? Php foreach ($ tree-> res as $ key => $ val) {echo"{$ Val}";}?>

I hope this article will help you with php programming.

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.