如何在树视图结构中显示数组?

How can I display an array in a tree view structure?

我想构建一个由部门(父)和子部门(子)组成的树视图。在我的数据库中,我认为我有一个像这样的好结构:

--------------------------------------------
 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 Development 和 Network 是 IT 部门的子部门。前端和后端是 Web 开发的子项。

我找到了一个递归函数,它适用于从此数据库中获取所有数据 table 并将它们放入正确结构的数组中。但问题是我不知道如何像树视图一样显示这个数组。 例如这样

.....等等....

在我的数据库中,我认为我有一个像这样的好结构:

我尝试使用

以非常简单的方式打印数组
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
            )
        )
    )
)

这是我的函数,它从数据库 table 的数组 $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));

如果你能帮助我解决这个问题并在 html 中显示一个树视图结构,我将非常感激我从函数 return 来自函数 buildTree($data) 的数组 $branch。

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>

您需要一个递归函数来检查对象的 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;
}

这将 return 一个在描述的层次结构中具有 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>