Php non-recursive tree array constructor

Source: Internet
Author: User
Php non-recursive tree array constructor
Share Two Tree array constructors that do not require recursive implementation. if you need them, give a reference.

Non-recursive tree array constructor, code:

     $ Item) {if ($ item [$ pid]) {if (! Isset ($ t [$ item [$ pid] ['parent'] [$ item [$ pid]) $ t [$ item [$ id] ['parent'] [$ item [$ pid] = & $ t [$ item [$ pid];} return $ t;}/***** create a subnode tree array * parameter ** $ ar array, data organized in the list of Joins * $ id array as the primary key subscript or associated key name * $ pid * array as the parent key subscript or associated key name * return multi-dimensional array ** /function find_child ($ ar, $ id = 'id', $ pid = 'pid ') {foreach ($ ar as $ v) $ t [$ v [$ id] = $ v; foreach ($ t as $ k =>$ item) {if ($ item [$ pid]) {$ t [$ item [$ pid] ['Ch Ild'] [$ item [$ id] = & $ t [$ k] ;}} return $ t ;} $ data = array ('id' => 1, 'parent' => 0, 'name' => 'grandpa'), array ('id' => 2, 'parent' => 1, 'name' => 'Father '), array ('id' => 3, 'parent' => 1, 'name' => 'Uncle '), array ('id' => 4, 'parent' => 2, 'name' => 'your '), array ('id' => 5, 'parent' => 4, 'name' => 'son'); $ p = find_parent ($ data, 'id ', 'parent'); $ c = find_child ($ data, 'id', 'parent'); print_r ($ c); e Xit;?>

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.