从目录树php获取json对象

时间:2014-02-03 23:17:05

标签: php json file tree

我有如下文件结构:

-problems
 - folder1.1
  -folder1.2
   -inputFolder
    -file.txt1
    -file2.txt2
   -outputFolder
   -file.txt1
   -file2.txt2
 -folder1.2
-folder2

我必须将它转换为两个json对象:

  problems = {
          name: "someNameChosenByMe",
          id: "someIdChosenByMe",
          tests: {
              input: [], // array of strings with contents from text files in input
              output: "" // string from files in output
          }

    }

这也是

tree = [
    {
      id          : "12" id of element from the other json
      parent      : "someDir" parent directory name
      text        : "someTextChosenByMe"
      icon        : "somethingChosenFromMe" 
    },
    {
      id          : "12" id of element from the other json
      parent      : "someDir" parent directory name
      text        : "someTextChosenByMe"
      icon        : "somethingChosenFromMe" 
    },
];

......等等。基本上我需要从第一个json的输入和输出文件夹中获取内容。而对于第二个我需要来自第一个json和父目录的元素的id。 我尝试了很多东西,但我不知道这个方法是什么,应该是什么。

$path = 'problems/';
$files = [];
$di = new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS);
$it = new RecursiveIteratorIterator($di);

foreach ($it as $file) {
    if (pathinfo($file, PATHINFO_EXTENSION) == "txt")
        $files[] = $file;
}

这为我提供了所有可能的文件路径。但我不知道如果最好的话我会继续这样做。那么这个方法是什么呢?我不想要任何代码,一般只做什么。我需要第二个json将它用于jstree.js。 http://www.jstree.com/docs/json

0 个答案:

没有答案