简体   繁体   English

使用分隔符在 PHP 中创建 JSON 树视图

[英]Create a JSON tree view in PHP using delimiter

I am trying to create a file explorer type treeview JSON to be read by FancyTree for a project I'm attempting.我正在尝试为我正在尝试的项目创建一个文件浏览器类型 treeview JSON 以供 FancyTree 读取。

The files are stored in a database, with an ID, name, URL, Type and code fields.这些文件存储在数据库中,具有 ID、名称、URL、类型和代码字段。 The mock database looks like this:模拟数据库如下所示:

ID      name        URL.        Type            code
1       test        dir.dir1    txt             sometext
2       next        dir.dir1    txt             somemoretext
3       main        dir         txt            evenmoretext

I need to build the JSON tree view from this data, using the URL as a path (period being the delimiter) and the files being inside the final directory so the tree looks like我需要从这些数据中构建 JSON 树视图,使用 URL 作为路径(句点是分隔符)并且文件位于最终目录中,因此树看起来像

/dir/dir1/test.txt
/dir/dir1/next.txt
/dir/main.txt

FancyTree JSON output should look like FancyTree JSON output 应该看起来像

[
    {
        "title": "dir",
        "folder": true,
        "children": [
            {
                "title": "dir1",
                "folder": true,
                "children": [
                    {
                        "title": "test.txt",
                        "key": 1
                    }, {
                        "title": "next.txt",
                        "key": 2
                    }
                ]
            }, {
                "title": "main.txt",
                "key": 3
            }
        ]
    }
]

Currently, I'm getting the data from the database into $scriptArray目前,我正在将数据库中的数据放入 $scriptArray

SELECT 'name','url','type','id' FROM.....

I'm then sorting and building a tree with然后我正在排序并构建一棵树

    $url = array_column($scriptArray, 'url');
    array_multisort($url, SORT_ASC, $scriptArray);

    $result = [];

    foreach($scriptArray as $item) {
        $loop = 0;
        $keys = array_reverse(explode('.', $item->url));
        $tmp = $item->name;
        $tmp2 = $item->type;

        foreach ($keys as $keyid => $key) {
            if($loop == 0) {
                $tmp = ["title" => $tmp.".".$tmp2, 'key' => $item->id];
            } else {
                $tmp = ["title" => $keys[$keyid - 1], "folder" => true, "children" => [$tmp]];
            }
            $loop++;
        }
        $tmp = ["title" => $keys[count($keys)-1], "folder" => true, "children" => [$tmp]];

        $result[] = $tmp;
    }

However, the output I'm getting is.但是,我得到的 output 是。

[
    {
        "title": "dir",
        "folder": true,
        "children": [
            {
                "title": "dir2",
                "folder": true,
                "children": [
                    {
                        "title": "test.txt",
                        "key": 1
                    }
                ]
            }
        ]
    },
    {
        "title": "dir",
        "folder": true,
        "children": [
            {
                "title": "dir2",
                "folder": true,
                "children": [
                    {
                        "title": "next.txt",
                        "key": 2
                    }
                ]
            }
        ]
    },
    {
        "title": "main.txt",
        "key": 3
    }
]

I have tried applying an array_merge, array_merge_recursive and various others without success.我曾尝试应用 array_merge、array_merge_recursive 和其他各种但没有成功。 Can anyone help with this?有人能帮忙吗?

Working with loop won't work unless you know per advance the maximum depth of your folder hierarchy.除非您事先知道文件夹层次结构的最大深度,否则使用循环将不起作用。

A better solution is to build the folder path "recursively", and append the file to the final folder.更好的解决方案是“递归”构建文件夹路径,并将 append 文件放到最终文件夹中。

This can be achieved with by creating a reference with the & operator, and navigate to its children until the whole path is build:这可以通过使用&操作符创建一个引用来实现,并导航到它的子节点直到整个路径被构建:

$result = array();
foreach($files as $file)
{
    // build the directory path if needed
    $directories = explode('.', $file->url); // get hierarchy of directories
    $currentRoot = &$result ; // set the pointer to the root directory per default
    foreach($directories as $directory)
    {
        // check if directory already exists in the hierarchy
        $dir = null ;
        foreach($currentRoot as $i => $d)
        {
            if($d['folder'] and $d['title'] == $directory)
            {
                $dir = &$currentRoot[$i] ;
                break ;
            }
        }
        
        // create directory if missing
        if(is_null($dir))
        {
            $item =  array(
                'title' => $directory,
                'folder' => true,
                'children' => array()
            );
            $currentRoot[] = $item ;
            $dir = &$currentRoot[count($currentRoot)-1];
        }
        
        // move to the next level
        $currentRoot = &$dir['children'] ; 
        unset($dir);
    }
    
    // finally append the file in the latest directory
    $currentRoot[] = array(
        'title' => $file->name . '.' . $file->type,
        'key' => $file->id,
    );
    
    
    unset($currentRoot);
}

echo json_encode($result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM