-1
我试着去生产这种用于JSTree目录迭代器自定义阵列
[
{"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"}]
我只需要图出去马国王它的子文件夹,子迭代现在
“我只需要弄清楚它现在在子子文件夹中迭代” - 阅读递归,应该这样做。 – Avalanche
感谢:D到达那里(添加答案),并与我明白的代码!我遇到了麻烦(仍不明白它)与此行'$路径=数组($ ritit-> getSubIterator($深度) - >当前() - > getFilename()=> $路径); ' – user1842842