PHP tree-no recursion required, php tree recursion _ PHP Tutorial

Source: Internet
Author: User
PHP tree-no recursion is required. php tree recursion. PHP tree-no recursion is required. php tree recursion ** creates a parent node tree array * parameter * $ ar array, data organized in the list of Joins * $ id array as the primary key subscript or associated key name PHP tree-no recursion is required, php tree recursion
/*** Create a parent node tree array * parameter * $ ar array, data organized in the list of Joins * $ the subscript or associated key name of the primary key in the id array * $ the subscript or associated key name of the parent key in the pid array * returns a multi-dimensional array **/ function find_parent ($ ar, $ id = 'id', $ pid = 'pid ') {foreach ($ ar as $ v) $ t [$ v [$ id] = $ v; foreach ($ t as $ k =>$ 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 * $ the subscript or associated key name of the primary key in the id array * $ the subscript or associated key name of the parent key in the pid array * returns a 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] ['child '] [$ 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 );

Execution result:


Array ([1] => Array ([ID] => 1 [PARENT] => 0 [NAME] => grandfather [child] => Array ([2] => Array ([ID] => 2 [PARENT] => 1 [NAME] => father [child] => Array ([4] => Array ([ID] => 4 [PARENT] => 2 [NAME] => yourself [child] => Array ([5] => Array ([ID] => 5 [PARENT] => 4 [NAME] => son ))))) [3] => Array ([ID] => 3 [PARENT] => 1 [NAME] => Uncle ))) [2] => Array ([ID] => 2 [PARENT] => 1 [NAME] => PARENT [child] => Array ([4] => Array ([ ID] => 4 [PARENT] => 2 [NAME] => yourself [child] => Array ([5] => Array ([ID] => 5 [PARENT] => 4 [NAME] => son ))))) [3] => Array ([ID] => 3 [PARENT] => 1 [NAME] => uncle) [4] => Array ([ID] => 4 [PARENT] => 2 [NAME] => yourself [child] => Array ([5] => Array ([ ID] => 5 [PARENT] => 4 [NAME] => son ))) [5] => Array ([ID] => 5 [PARENT] => 4 [NAME] => son ))

Parameters/*** create a parent node tree array * parameter * $ ar array, the data organized by the list of neighboring nodes * $ id array as the primary key subscript or associated key name...

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.