Infinite pole structure loops, no array functions, one loop, and fast filtering based on the root given

Source: Internet
Author: User

<?php
$nodesArrays = [
[' id ' = 1, ' pid ' = + 0, ' name ' = ' a '],
[' id ' = 2, ' pid ' = + 0, ' name ' = ' B '],
[' id ' = 3, ' pid ' = + 1, ' name ' = ' C '],
[' id ' = 4, ' pid ' = + 2, ' name ' = ' d '],
[' id ' = 5, ' pid ' = + 6, ' name ' = ' E '],
[' id ' = 6, ' pid ' = + 4, ' name ' = ' F '],
[' id ' = 7, ' pid ' = + 6, ' name ' = ' g '],
[' id ' = 8, ' pid ' = + 6, ' name ' = ' h '],
[' id ' = 9, ' pid ' = ', ' name ' = ' I '],
[' id ' = +, ' pid ' = + 8, ' name ' = ' J '],
[' id ' = +, ' pid ' = + 6, ' name ' = ' l '],
[' id ' = +, ' pid ' = + 7, ' name ' = ' m '],
];

$nodesArray 1 = [
[' id ' = 1, ' pid ' = + 4, ' name ' = ' a '],
[' id ' = 2, ' pid ' = + 0, ' name ' = ' B '],
[' id ' = 3, ' pid ' = + 1, ' name ' = ' C '],
[' id ' = 4, ' pid ' = + 2, ' name ' = ' d '],
[' id ' = 5, ' pid ' = + 6, ' name ' = ' E '],
[' id ' = 6, ' pid ' = + 4, ' name ' = ' F '],
[' id ' = 7, ' pid ' = + 6, ' name ' = ' g '],
[' id ' = 8, ' pid ' = + 6, ' name ' = ' h '],
[' id ' = 9, ' pid ' = ', ' name ' = ' I '],
[' id ' = +, ' pid ' = + 8, ' name ' = ' J '],
[' id ' = +, ' pid ' = + 6, ' name ' = ' l '],
[' id ' = +, ' pid ' = + 7, ' name ' = ' m '],
];

//Change the idea, do not change the source data, create a new root node, a child node tree
function GetTreeNode3 ($list, $root = 0, $PK = ' id ', $pid = ' pid ', $child = ' _child ') { br>//Create root node
$root _tree = Array ();
Create child node
$node _tree = Array ();
Lost node
$no _tree = Array ();
if (Is_array ($list)) {
//Since the array function cannot be used to change the subscript of the source data, then the subscript of the node is placed on the child node tree
foreach ($list as $key + = $data) {
// Determines if there is a parent
$parentId = $data [$pid];
$nowId = $data [$PK];
//Determine the root node array based on the $root value, not 0 root problem
if ($root = = $parentId && $root = = 0) {
$node _tree[$nowId] = $data;
$root _tree[$nowId] = & $node _tree[$nowId];
}else if ($root = = $nowId && $root! = 0) {
$node _tree[$nowId] = $data;
$root _tree[$nowId] = & $node _tree[$nowId];
if (isset ($no _tree[$nowId)) {
$node _tree[$nowId] [$child] = & $no _tree[$nowId];
}
}else {

            At the same time confirm whether the root node and child node both exist//Find child node//$flag _node_tree = false;                The root node is also present, the mapping parent is associated with the child node if (isset ($root _tree[$parentId]) && isset ($node _tree[$parentId])) {                At the same time, there are mapping parent-child relationships, changing sub-$root _tree[$parentId] = & $node _tree[$parentId];                $node _tree[$nowId] = $data;                $node _tree[$parentId] [$child] [$nowId] = & $node _tree[$nowId];                if (Isset ($no _tree[$nowId])) {$node _tree[$nowId] [$child] = & $no _tree[$nowId]; }}else if (!isset ($root _tree[$parentId]) && isset ($node _tree[$parentId])) {//The parent does not exist the child is written                Sub-$node _tree[$nowId] = $data;                $node _tree[$parentId] [$child] [$nowId] = & $node _tree[$nowId];                if (Isset ($no _tree[$nowId])) {$node _tree[$nowId] [$child] = & $no _tree[$nowId]; }}else if (Isset ($root _tree[$parentId]) &&!isset ($node _tree[$parentId])) {//parent node exists, and child nodes do not $node _tree[$nowId] = $data;                $root _tree[$parentId] [$child] [$nowId] = & $node _tree[$nowId];                if (Isset ($no _tree[$nowId])) {$root _tree[$parentId] [$child] = & $no _tree[$nowId];                }}else {$node _tree[$nowId] = $data;            $no _tree[$parentId] [$nowId] = & $node _tree[$nowId];        }}/*echo "\nroot_tree\n";        Print_r ($root _tree);        echo "\nnode_tree\n";        Print_r ($node _tree);        echo "\nno_tree\n"; Print_r ($no _tree); * *}}return $root _tree;

}

$treeval = GetTreeNode3 ($nodesArrays, 8);
Print_r ($treeval);

Infinite pole structure loops, no array functions, one loop, and fast filtering based on the root given

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.