数组转换成树php,如何在PHP中将此MPTT数组转换为树结构?

阅读: 评论:0

数组转换成树php,如何在PHP中将此MPTT数组转换为树结构?

数组转换成树php,如何在PHP中将此MPTT数组转换为树结构?

我在数据库中有分层数据,以Modified Preorder Tree Traversal格式存储.我在一个看起来类似“SELECT ID,Left,Right,Name等FROM FROM ORDER BY Left;”的查询中提取数据.我试图将这个数据从一个平面数组转换成DB给我一个树结构,然后我将用PHP的json_encode函数输出为JSON.

但是,我无法让我的树形结构代码超出第一级.这是一个最小的测试用例:

<?php 

function projectListToTree($projects) {

$stack = Array();

for($x =0; $x < count($projects); $x++) {

$project = $projects[$x];

$project['Children'] = Array();

while(count($stack) > 0 && $stack[count($stack) - 1]['Right'] < $project['Right']) {

array_pop($stack);

}

if(count($stack) > 0) {

$stack[count($stack) - 1]['Children'][] = $project;

echo "Adding " . $project['Name'] . " to " . $stack[count($stack) - 1]['Name'] . " for a total of "

. count($stack[count($stack) - 1]['Children']) . " kidsn";

} else {

echo "No parentn";

}

echo "stack count: " . count($stack) . "n";

array_push($stack, $project);

}

echo "Left in stack: " . count($stack) . "n";

return $stack[0];

}

/*

This is basically what comes from the DB.

Should be:

Parent

First Child

Second Child

Grand Child

*/

$projects = Array(

Array(

"ID" => "2",

"Left" => "2",

"Right" => "9",

"ParentID" => "1",

"Name" => "Parent"

),

Array(

"ID" => "3",

"Left" => "3",

"Right" => "4",

"ParentID" => "2",

"Name" => "First Child"

),

Array(

"ID" => "4",

"Left" => "5",

"Right" => "8",

"ParentID" => "2",

"Name" => "Second Child"

),

Array(

"ID" => "5",

"Left" => "6",

"Right" => "7",

"ParentID" => "4",

"Name" => "Grand Child"

)

);

$tree = projectListToTree($projects);

echo "-----nnnn";

var_dump($tree);

?>

这就是我得到的输出:

No parent

stack count: 0

Adding First Child to Parent for a total of 1 kids

stack count: 1

Adding Second Child to Parent for a total of 2 kids

stack count: 1

Adding Grand Child to Second Child for a total of 1 kids

stack count: 2

Left in stack: 3

-----

array(6) {

["ID"]=>

string(1) "2"

["Left"]=>

string(1) "2"

["Right"]=>

string(1) "9"

["ParentID"]=>

string(1) "1"

["Name"]=>

string(6) "Parent"

["Children"]=>

array(2) {

[0]=>

array(6) {

["ID"]=>

string(1) "3"

["Left"]=>

string(1) "3"

["Right"]=>

string(1) "4"

["ParentID"]=>

string(1) "2"

["Name"]=>

string(11) "First Child"

["Children"]=>

array(0) {

}

}

[1]=>

array(6) {

["ID"]=>

string(1) "4"

["Left"]=>

string(1) "5"

["Right"]=>

string(1) "8"

["ParentID"]=>

string(1) "2"

["Name"]=>

string(12) "Second Child"

["Children"]=>

array(0) {

}

}

}

}

正如你所看到的,即使projectListToTree函数中的输出似乎表明它应该存在,某个地方“孙子”也会迷路.看起来我抛出的任何树形结构都会降低第二级以下的任何结构.对可能发生的事情的任何见解?

谢谢!

解决方法:

问题是分配数组不会复制引用,而是复制数组.这意味着,您在“父”节点的“子”中拥有的“第二个子”数组与您添加“孙子”的数组不同,而是它的副本.

要解决此问题,您必须显式使用引用分配而不是复制:

function projectListToTree($projects) {

$stack = Array();

for($x =0; $x < count($projects); $x++) {

$project = &$projects[$x];

$project['Children'] = array();

while(count($stack) > 0 && $stack[count($stack) - 1]['Right'] < $project['Right']) {

array_pop($stack);

}

if(count($stack) > 0) {

$stack[count($stack) - 1]['Children'][] = &$project;

echo "Adding " . $project['Name'] . " to " . $stack[count($stack) - 1]['Name'] . " for a total of "

. count($stack[count($stack) - 1]['Children']) . " kidsn";

echo "n";

} else {

echo "No parentn";

}

echo "stack count: " . count($stack) . "n";

$stack[] = &$project;

}

echo "Left in stack: " . count($stack) . "n";

return $stack[0];

}

请注意,在三个地方添加了&符号.

由于这个问题,在php中使用嵌套数组和赋值运算符时必须非常小心.

这也意味着在嵌套数组中使用大量数据时,处理器使用率和内存占用量很大.例如,在上面的例子中,当projectListToTree()返回时,完整的数组树被复制到局部变量$tree并且(因为php垃圾收集器很糟糕)在内存中两次.

标签:php,arrays,tree,mptt

来源: .html

本文发布于:2024-02-02 05:46:01,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170682396241753.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

上一篇:Django 博客
下一篇:使用 sqlalchemy
标签:数组   将此   转换成   转换为   结构
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23