目录迭代器到自定义数组


Directory Iterator To Custom Array

我试图为jtree生成这个

[
            {"id":2,"text":"Child node 1"},
            {"id":3,"text":"Child node 2"},
            {
                "text" : "Root node",
                "state" : { "opened" : true },
                "children" : [
                    {
                        "text" : "Child node 1",
                        "state" : { "selected" : true },
                        "icon" : "jstree-file"
                    },
                    { "text" : "Child node 222", "state" : { "disabled" : true } }
                ]
            }
        ]

我试图通过使用DirectoryIterator

创建这个

我从php网站

获得了以下内容
   $ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory), FilesystemIterator::SKIP_DOTS); 
    $r = array(); 
    foreach ($ritit as $splFileInfo) { 

        $path = $splFileInfo->isDir() 
         ? array($splFileInfo->getFilename() => array()) 
         : array($splFileInfo->getFilename()); 
       for ($depth = $ritit->getDepth() - 1; $depth >= 0; $depth--) { 
       $path = array($ritit->getSubIterator($depth)->current()->getFilename() => $path); 
       } 
       $r = array_merge_recursive($r, $path); 
    } 

输出

数组((。] =>数组()

[..] => Array
(
)
[0] => Christmas.docx
[test1] => Array
    (
    )
[test2] => Array
    (
    )
)

我试着把它修改成这样

$k = array(); 
foreach ($ritit as $splFileInfo) { 
    //skip if its '.' or '..'
    if ($splFileInfo->getFilename() === '.' || $splFileInfo->getFilename() === '..') {
    continue;
    }
    //if is dir
   if($splFileInfo->isDir()) {
        $path[] = array("text" => $splFileInfo->getFilename(), "children" => array()) ;
        } else {
             $path[] = array("text" => $splFileInfo->getFilename());
    }        
   for ($depth = $ritit->getDepth() - 1; $depth >= 0; $depth--) { 
   $path["children"] = array($ritit->getSubIterator($depth)->current()->getFilename() => $path); 
   } 
   $k = array_merge_recursive($k, $path); 
} 

输出

Array
(
    [test2] => Array
        (
        )
    [0] => Array
        (
            [text] => Christmas.docx
        )
    [1] => Array
        (
            [text] => Christmas.docx
        )
    [2] => Array
        (
            [text] => test1
            [children] => Array
                (
                )
        )
    [3] => Array
        (
            [text] => Christmas.docx
        )
    [4] => Array
        (
            [text] => test1
            [children] => Array
                (
                )
        )
    [5] => Array
        (
            [text] => test2
            [children] => Array
                (
                )
        )
)

请你能帮我做什么,我要做的是通过文件结构迭代,并返回一个数组,我可以转换成json在帖子的开始?


更新

归功于:https://stackoverflow.com/a/3556894/1842842

我现在明白了:

function requestAccountFolderStructure($dir) {
    $list = array();
    $path = $dir;
    $i = 0;
    foreach (new DirectoryIterator($path) as $file) {
        if ($file->isDot())
            continue;
        if ($file->isDir()) {
            $record = array();
            $record['id'] =  $i;
            $record['text'] =  $file->getFilename();
            $record['children'] = array();            
            $record['path'] = $file->getPathInfo()->getFilename();            
            if (is_dir($dir . '/' . $file)) {
                $record['children'] = requestAccountFolderStructure($dir . '/' . $file);
            }
            $list[] = $record;
            $i++;
        }
    }
    return $list;
}
   $dir = 'folder/';
   $directories = requestAccountFolderStructure($dir);
   foreach($directories as $directory){
       //if(count($directory['children'] === 1)){
           $dir = new DirectoryIterator('folder/' . $directory['text']);
            foreach ($dir as $fileinfo) {
        if (!$fileinfo->isDir()) {
                    $directories[$directory['id']]['children'][] = array("text" => $fileinfo->getFilename(),"icon" => "jstree-file" );
        }
            }
       //}     
   }  
   print_r(json_encode($directories));  

为我产生

[{"id":0,"text":"test1","children":[{"text":"1.docx","icon":"jstree-file"}],"path":"folder"},{"id":1,"text":"test2","children":[{"id":0,"text":"New folder","children":[],"path":"test2"},{"text":"2.txt","icon":"jstree-file"}],"path":"folder"}]

我只需要弄清楚如何让它在子文件夹中迭代

来源:https://stackoverflow.com/a/952324/1842842

此代码

$fileData = convertDirectoryToArray( new DirectoryIterator( 'folder/' ) );
function convertDirectoryToArray( DirectoryIterator $dir )
{
  $data = array();
  foreach ( $dir as $node )
  {
    if ( $node->isDir() && !$node->isDot() )
    {
      $test = array();
      $test['text'] = $node->getFilename();
      $test['children'] = convertDirectoryToArray( new DirectoryIterator( $node->getPathname() ) );
      $data[] = $test;
    }
    else if ( $node->isFile() )
    {
      $test= array();
      $test['text'] = $node->getFilename();
      $test['icon'] = "jstree-file";
      $data[] = $test;
    }
  }
  return $data;
}
print_r(json_encode($fileData)); 

生产

[{"text":"Christmas.docx"},{"text":"test1","children":[{"text":"1.docx"}]},{"text":"test2","children":[{"text":"2.txt"},{"text":"New folder","children":[{"text":"4.txt"}]}]}]

这就是我要找的!