Php 将嵌套集转换为数据(json/html),以便在jstree中使用

Php 将嵌套集转换为数据(json/html),以便在jstree中使用,php,arrays,jstree,Php,Arrays,Jstree,我有一个带有表的数据库,其中数据作为嵌套集存储。该表如下所示: id lft rgt name depth 0 1 768 ROOT 0 550300 2 3 external 1 580943 4 5 teachers 1 510000 6 753 company BV 1 213000 7 14

我有一个带有表的数据库,其中数据作为嵌套集存储。该表如下所示:

id      lft rgt name                  depth
0       1   768 ROOT                  0
550300  2   3   external              1
580943  4   5   teachers              1
510000  6   753 company BV            1
213000  7   14  Buying                2
913010  8   9   buying center         3
573100  10  11  buying generic        3
516300  12  13  buying service center 3
513900  15  48  finance               2
<div id="html1">
  <ul>
    <li>Root node 1
      <ul>
        <li>Child node 1</li>
        <li><a href="#">Child node 2</a></li>
      </ul>
    </li>
  </ul>
</div>
数据代表一个公司结构。我想用jstree显示它

我认为最好的方法是首先把它放在多维数组中。为此,我使用了此函数:

function _formatTreeIntoArray(&$tree, $current_depth = 0)
{
    $formattedTree = array();
    while($leaf = current($tree))
    {
        if($leaf['depth'] > $current_depth)
        {
            $formattedTree[] = _formatTreeIntoArray($tree, $leaf['depth']);
        }
        elseif($leaf['depth'] < $current_depth)
        {
            return $formattedTree;
        }
        else
        {
            $formattedTree[] = $leaf;
            next($tree);
        }
    }

    return $formattedTree;
}
我在本主题中找到了一个函数:,但由于将嵌套集转换为数组的函数创建了许多没有键名称的数组项,因此我在树中获得了很多额外的级别,其中只有一个数字作为名称,并且没有适当的子级


我想知道如何解决这个问题,以及是否有更好的方法来实现我的目标。

有两种格式可用于将JSON对象传递给jsTree以构建树。 我会使用第二个“扁平”的,如下图所示。当然,您必须在服务器端构建它

[
  { "id": "root", "parent": "#", "text": "ROOT" },
  { "id": "external", "parent": "root", "text": "external" },
  { "id": "teachers", "parent": "root", "text": "teachers" },
  { "id": "companyBV", "parent": "root", "text": "company BV" },
  { "id": "buying", "parent": "companyBV", "text": "Buying" },
  { "id": "finance", "parent": "companyBV", "text": "finance" },
  { "id": "buyingCenter", "parent": "buying", "text": "buying center" },
  { "id": "buyingGeneric", "parent": "buying", "text": "buying generic" },
  { "id": "buyingSCenter", "parent": "buying", "text": "buying service center" }
]
在客户端,只需将其馈送到jsTree配置:

$('#jstree').jstree({
    core: {
      data: data
    }
})

检查demo-

有两种格式可用于将JSON对象传递给jsTree以构建树。 我会使用第二个“扁平”的,如下图所示。当然,您必须在服务器端构建它

[
  { "id": "root", "parent": "#", "text": "ROOT" },
  { "id": "external", "parent": "root", "text": "external" },
  { "id": "teachers", "parent": "root", "text": "teachers" },
  { "id": "companyBV", "parent": "root", "text": "company BV" },
  { "id": "buying", "parent": "companyBV", "text": "Buying" },
  { "id": "finance", "parent": "companyBV", "text": "finance" },
  { "id": "buyingCenter", "parent": "buying", "text": "buying center" },
  { "id": "buyingGeneric", "parent": "buying", "text": "buying generic" },
  { "id": "buyingSCenter", "parent": "buying", "text": "buying service center" }
]
在客户端,只需将其馈送到jsTree配置:

$('#jstree').jstree({
    core: {
      data: data
    }
})

检查demo-

谢谢Nikolay,这种结构确实让查询变得容易了一些,我在本主题中找到了一个很好的查询,可以从数据库中以一种格式获取结果,在这种格式中我可以获取子id和父id,然后我可以将其用于jstree。谢谢Nikolay,这种结构确实让查询变得容易了一些,我在本主题中找到了一个很好的查询,可以从数据库中以一种格式获取结果,在这种格式中,我可以获取子id和父id,然后将其用于jstree。