我试图为JSTree生成这个
.container {
width: 200px;
height: 100px;
padding: 10px;
border: 1px solid #000;
position: relative;
}
.block {
width: 100px;
height: 100px;
background: red;
position: absolute;
animation: move 2s linear infinite;
}
@keyframes move {
0% { left: 10px; }
25% { left: 50px; }
50% { left: 100px; }
75% { left: 50px; }
100% { left: 10px; }
}
我试图通过使用DirectoryIterator
来创建它我从php网站上得到了以下内容
[
{"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 } }
]
}
]
此输出
阵 ( [。] =>排列 ( )
$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);
}
请你帮我解决一下我想要做什么来迭代文件结构并返回一个我可以在帖子开头转换成json的数组?
更新
信用到:https://stackoverflow.com/a/3556894/1842842
我现在有了这个:
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
(
)
)
)
哪个为我生产
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));
我只需要弄清楚它现在在子子文件夹中迭代
答案 0 :(得分:1)
信用: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"}]}]}]
我正在寻找的是什么!