【问题标题】:Nestable List Plugin PHP MYSQlL Json Tree structure可嵌套列表插件 PHP MYSQlL Json 树结构
【发布时间】: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
        )

)

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

【问题讨论】:

    标签: php mysql json list


    【解决方案1】:

    好的,多亏了 --> 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 和无限父级的解决方案,这些父级适用于可嵌套列表插件。从数组创建 JSON 使用json_encode($tree,JSON_UNESCAPED_UNICODE);

    :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多