PHP sharing _php instances without recursive implementation of infinite grading examples

Source: Internet
Author: User
Two tree-shaped array manipulation functions without recursion

Copy the Code code as follows:
/**
* Create parent Node tree array
* Parameters
* $ar arrays, adjacency list-organized data
* $id the subscript or association key name in the array as the primary key
* $pid the subscript or association key name in the array as the parent key
* Returns a multidimensional 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 sub-node tree array
* Parameters
* $ar arrays, adjacency list-organized data
* $id the subscript or association key name in the array as the primary key
* $pid the subscript or association key name in the array as the parent key
* Returns a multidimensional 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;
}

Examples of Use:

Copy the Code code as follows:
$data = Array (
Array (' ID ' =>1, ' PARENT ' =>0, ' NAME ' = ' grandfather '),
Array (' ID ' =>2, ' parent ' =>1, ' NAME ' = ' father '),
Array (' ID ' =>3, ' PARENT ' =>1, ' NAME ' = ' uncles '),
Array (' ID ' =>4, ' PARENT ' =>2, ' NAME ' = ' own '),
Array (' ID ' =>5, ' PARENT ' =>4, ' NAME ' = ' son '),
);

$p = Find_parent ($data, ' ID ', ' parent ');
$c = Find_child ($data, ' ID ', ' PARENT ');


Execution effect:

Copy CodeThe code is as follows:
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] = = Himself
[Child] = = Array
(
[5] = = Array
(
[ID] = 5
[PARENT] = 4
[NAME] = son
)

)

)

)

)

[3] = = Array
(
[ID] = 3
[PARENT] = 1
[NAME] = uncles
)

)

)

[2] = = Array
(
[ID] = 2
[PARENT] = 1
[NAME] = Father
[Child] = = Array
(
[4] = = Array
(
[ID] = 4
[PARENT] = 2
[NAME] = = Himself
[Child] = = Array
(
[5] = = Array
(
[ID] = 5
[PARENT] = 4
[NAME] = son
)

)

)

)

)

[3] = = Array
(
[ID] = 3
[PARENT] = 1
[NAME] = uncles
)

[4] = = Array
(
[ID] = 4
[PARENT] = 2
[NAME] = = Himself
[Child] = = Array
(
[5] = = Array
(
[ID] = 5
[PARENT] = 4
[NAME] = son
)

)

)

[5] = = Array
(
[ID] = 5
[PARENT] = 4
[NAME] = son
)

)

  • Related Article

    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.