【发布时间】:2015-04-30 06:22:33
【问题描述】:
我的第一个问题。我正在使用 Nestable List Plugin 并尝试从数据库中获取相同的结构。
数据库长这样(数据源) (tobad需要10声望) 这种结构
ID:1
家长编号:0
文字:Hello World
类型:文本
----新行
编号:2
家长编号:1
文字:儿童:D
类型:文本
等等。,不知道它的名字,但那是一种层次结构。 我正在尝试将其转换为具有无限 lvls 的类似内容: [{"id":13},{"id":14},{"id":15,"children":[{"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
)
)
希望这个问题有意义吗?,我可以进一步解释,请问。谢谢
【问题讨论】: