gpt4 book ai didi

php - Nestable List 插件 PHP MYSQL L Json 树结构

转载 作者:行者123 更新时间:2023-11-29 22:29:12 25 4
gpt4 key购买 nike

我的第一个问题。我正在使用Nestable List Plugin并尝试从数据库获取相同的结构。

数据库看起来像这样(数据源)(托巴德需要10声望)这种结构

ID:1家长 ID: 0文字: Hello World
类型:文本----新行编号:2家长ID:1文字: child :D类型:文本

等等,不知道它的名称,但这是一种层次结构。我正在尝试将其转换为类似的东西,具有无限的等级:[{“id”:13},{“id”:14},{“id”:15,“ child ”:[{“id”:16},{“id”:17},{“id”: 18}]}]

这是我迄今为止的尝试

PHP 端

if($productSQL[0])
{
foreach($productSQL[1]['obj_ID'] as $iIDKey => $iIDValues)
{

if($productSQL[1]['objProductIDArr'][$iIDKey] == 0)
{
// Type Text
$objData = $getDataInspection->listTemplate((object) ['resellerID' => $calcObj['resellerID'],'action' => 'objectByID','objectID' => $productSQL[1]['ii_object_id'][$iIDKey]]);
$type = "text";

// Push values into arrays
array_push($idArr, $productSQL[1]['obj_ID'][$iIDKey]);
array_push($textArr, $objData[0]['Value'][0]);
array_push($contentType, $type);
array_push($parentIDArr, $productSQL[1]['obj_parentID'][$iIDKey]);

}
else
{
// Type Table
$type = "table";
}

}

$jsonHolder['id'] = $idArr;
$jsonHolder['text'] = $textArr;
$jsonHolder['parentID'] = $parentIDArr;
$jsonHolder['type'] = $contentType;

$test = $functions->buildTree($jsonHolder);

print_r($test);
}
else
{
// No data present
$json = "empty";
}

将它们保存到数组中,然后再次将它们保存到单个数组中然后将其进一步放入我尝试从 Veerendra 实现但没有成功的 buldTree 函数 PHP - How to build tree structure list?

功能

// Got this function from Stackoverflow :), thanks Veerendra :=
function buildTree(array $elements, $parentId = 0) {
$branch = array();
foreach($elements['id'] as $elementsKey => $elementValues)
{
if ($elements['parentID'][$elementsKey] == $parentId)
{

$jsonArr['id'] = $elementValues;
$jsonArr['parentID'] = $elements['parentID'][$elementsKey];
$jsonArr['text'] = $elements['text'][$elementsKey];
$jsonArr['type'] = $elements['type'][$elementsKey];

$children = $this->buildTree($elements, $elementValues);
if ($children)
{
$elements['children'] = $jsonArr;
}

$branch[] = $jsonArr;
}
}
return $branch;
}

这是迄今为止 $branch 的结果

Array
(
[0] => Array
(
[id] => 219
[parentID] => 0
[text] => butik
[type] => text
)

[1] => Array
(
[id] => 244
[parentID] => 0
[text] => kontor
[type] => text
)

)

希望这个问题有意义吗?,我可以进一步解释 afcorse,只是问。谢谢

最佳答案

好的,感谢 --> Pierre de LESPINAY,我明白了 https://stackoverflow.com/a/22020668/1912618

if($productSQL[0])
{
foreach($productSQL[1]['obj_ID'] as $iIDKey => $iIDValues)
{
if($productSQL[1]['objProductIDArr'][$iIDKey] == 0)
{
// Type Text
$objData = $getDataInspection->listTemplate((object) ['resellerID' => $calcObj['resellerID'],'action' => 'objectByID','objectID' => $productSQL[1]['ii_object_id'][$iIDKey]]);
$type = "text";
$arr[] = ['id' => $productSQL[1]['obj_ID'][$iIDKey], 'parentid' => $productSQL[1]['obj_parentID'][$iIDKey], 'name' => $objData[0]['Value'][0]];
}
else
{
// Type Table
$type = "table";

}
}
$tree = $functions->createTree($arr);
print_r($tree);
}
else
{
// No data present
$json = "empty";
}


// Class
/* Recursive branch extrusion */
function createBranch(&$parents, $children) {
$tree = array();
foreach ($children as $child) {
if (isset($parents[$child['id']])) {
$child['children'] =
$this->createBranch($parents, $parents[$child['id']]);
}
$tree[] = $child;
}
return $tree;
}

/* Initialization */
function createTree($flat, $root = 0) {
$parents = array();
foreach ($flat as $a) {
$parents[$a['parentid']][] = $a;
}
return $this->createBranch($parents, $parents[$root]);
}

此代码为我提供了创建无限级别和无限父项的解决方案,可使用嵌套列表插件。从数组创建 JSON使用json_encode($tree,JSON_UNESCAPED_UNICODE);

:)

关于php - Nestable List 插件 PHP MYSQL L Json 树结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29960412/

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