我的第一个问题。我正在使用 Nestable List Plugin 并尝试从数据库中获取相同的结构。
数据库看起来像这样(数据源) (tobad需要10个声望) 这种结构
ID:1
ParentID:0
文字:Hello World
类型:文字
----新排
ID:2
ParentID:1
文字:儿童:D
输入:text
等等。,不知道它的名称,但这是一种层次结构。 我试图将其转换成这样的东西,无限的lvls: [{" ID":13},{" ID":14},{" ID":15,"的子":[{ " ID":16},{" ID":17},{" ID":18}]}]
这是我到目前为止的尝试
PHP Side
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实现但没有succsess的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
)
)
希望这个问题有任何意义吗?我可以进一步解释,只要问一下。感谢
答案 0 :(得分:0)
好的,我得到了感谢 - > 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]);
}
这段代码为我提供了创建无限lvls和无限制父母的解决方案,可以使用Nestable List Plugin。从数组创建JSON
使用json_encode($tree,JSON_UNESCAPED_UNICODE);
:)