PHP does not recursively implement an infinite rating example sharing _php instances

Source: Internet
Author: User

Two tree-shaped array operation functions without recursion

Copy Code code as follows:

/**
* Create parent Node tree array
* Parameter
* $ar Array, adjacency list of data organized by
* $id subscript or associated key name in the array as the primary key
* $pid subscript or associated key name in the array as the parent key
* Return 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 child node tree array
* Parameter
* $ar Array, adjacency list of data organized by
* $id subscript or associated key name in the array as the primary key
* $pid subscript or associated key name in the array as the parent key
* Return 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;
}

Use examples:

Copy 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 ' => ' Uncle '),
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 Code code 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] => yourself
[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] => yourself
[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] => yourself
[Child] => Array
(
[5] => Array
(
[ID] => 5
[PARENT] => 4
[NAME] => son
)

)

)

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

)

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.