gpt4 book ai didi

php - jqtree json数据与php和mysql

转载 作者:行者123 更新时间:2023-11-29 13:31:34 26 4
gpt4 key购买 nike

如何使用 php 生成对 jqtree 的 json 响应?我尝试过 json_encode,但它留下了很多额外的数据。我想知道是否有人已经 pry 开了这扇门。

如何实现这样的结构:

var data = [
{
label: 'node1',
children: [
{ label: 'child1' },
{ label: 'child2' }
]
},
{
label: 'node2',
children: [
{ label: 'child3' }
]
}
];

形成mysql id,parent,lable结构?

任何帮助将不胜感激

最佳答案

json字符串中的“额外数据”是由于json_encode和关联数组造成的。如果您尝试对 json_encode 和索引数组进行编码,您将获得一个完全不同的 json 字符串,并且没有“标签”。

但是,jqtree 需要一个混合 json,其中包含一些“字符串”键和一些“索引”键。

然后,为了在 PHP 中获取 jqtree 的有效 json,您应该对查询结果数组使用以下函数,然后对该函数返回的数组进行 json_encode:

    function arrayValuesRecursive($array)
{
$temp = array();
foreach ($array as $key => $value) {
if (is_numeric($key)) {
$temp[] = is_array($value) ? arrayValuesRecursive($value) : $value;
} else {
$temp[$key] = is_array($value) ? arrayValuesRecursive($value) : $value;
}
}

return $temp;
}

例如:

$arrayFromQuery = array(...);
$arrayFromQuery = arrayValuesRecursive($arrayFromQuery);
$jqTreeJSON = json_encode($arrayFromQuery);

关于php - jqtree json数据与php和mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19381587/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com