我想要构建一个由部门(父部门)和子部门(子部门)组成的树状视图。在我的数据库中,我认为我有一个很好的结构,如下所示:
--------------------------------------------
Dep_name | dep_id | dep_parent_id
--------------------------------------------
Accounting | 1 | 0
Human-Resources | 2 | 0
IT | 3 | 0
Network | 4 | 3
Web Development | 5 | 3
Front-End | 6 | 5
Back-End | 7 | 5拥有dep_parent_id 0的部门->它们没有父部门。例如,Web开发和网络是IT部门的子类。前端和后端是Web开发的子程序。
我已经找到了一个递归函数,它适合于从这个数据库表中获取所有数据,并将它们放在一个数组中,并具有正确的结构。但问题是我不知道如何像树视图一样显示这个数组。例如,像这样
.等等..。
在我的数据库中,我认为我有一个很好的结构,如下所示:

我尝试以一种非常简单的方式打印数组
print_r($tree);然后印成这样:
Array (
[0] => stdClass Object (
[Dep_name] => Accounting and Finance
[dep_id] => 1
[dep_parent_id] => 0
)
[1] => stdClass Object (
[Dep_name] => Human-Recources
[dep_id] => 2
[dep_parent_id] => 0
)
[2] => stdClass Object (
[Dep_name] => IT
[dep_id] => 3
[dep_parent_id] => 0
[children] => Array (
[0] => stdClass Object (
[Dep_name] => Network
[dep_id] => 5
[dep_parent_id] => 3
)
[1] => stdClass Object (
[Dep_name] => Web Development
[dep_id] => 6
[dep_parent_id] => 3
[children] => Array (
[0] => stdClass Object (
[Dep_name] => Front-End
[dep_id] => 7
[dep_parent_id] => 6
)
[1] => stdClass Object (
[Dep_name] => Back-End
[dep_id] => 8
[dep_parent_id] => 6
)
)
)
)
)
[3] => stdClass Object (
[Dep_name] => Marketing
[dep_id] => 4
[dep_parent_id] => 0
[children] => Array (
[0] => stdClass Object (
[Dep_name] => web-marketing
[dep_id] => 9
[dep_parent_id] => 4
)
)
)
)这是我的函数,从数组$data中从数据库表获取数据,并构建树数组$branch。
function buildTree(array $data, $parentId = 0)
{
$branch = array();
foreach ($data as $element)
{
if ($element->dep_parent_id == $parentId)
{
$children = buildTree($data, $element->dep_id);
if ($children)
{
$element->children = $children;
}
$branch[] = $element;
}
}
return $branch;
}比我打印出来的还要多:
print_r(buildTree($data));如果您能帮助我解决这个问题,并从$branch数组buildTree($data)返回的html中显示一个树视图结构,我将非常感激。
发布于 2019-07-07 11:00:50
您需要一个递归函数来检查对象的children属性是否存在,如果存在,则在循环这些元素之前打印一个新的<ul>标记。
function printTree($array) {
$output = "<ul>\n";
foreach ($array as $a) {
$output .= "<li>".$a->Dep_name."</li>\n";
if (isset($a->children)) {
$output .= printTree($a->children);
}
}
$output .= "</ul>\n";
return $output;
}这将返回一个字符串,该字符串具有描述的层次结构中的HTML。输出将是这样的(嗯,不会像这样缩进,但是HTML会打印相同的内容)
<ul>
<li>Accounting and Finance</li>
<li>Human-Recources</li>
<li>IT</li>
<ul>
<li>Network</li>
<li>Web Development</li>
<ul>
<li>Front-End</li>
<li>Back-End</li>
</ul>
</ul>
<li>Marketing</li>
<ul>
<li>web-marketing</li>
</ul>
</ul>
发布于 2019-07-07 10:49:43
function sort(array $array): array
{
$sort = [];
foreach($array as $item) {
if ($item->dep_parent_id !== 0) {
$sort[$item->dep_parent_id][$item->dep_id] = $item->Dep_name;
continue;
}
$sort[$item->dep_id][0] = $item->Dep_name;
}
return $sort;
}
<ul>
<?php foreach (sort($array) as $item): ?>
<li><?= $item[0]?></li>
<?php if (count($item) > 1): ?>
<ul>
<?php unset($item[0]); ?>
<?php foreach($item as $value): ?>
<li><?= $value ?></li>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php endforeach ?>
</ul>https://stackoverflow.com/questions/56921377
复制相似问题