2
我試圖列出目錄並生成一棵樹。 我有這個功能,但問題是他給我第一個空陣列。 當傳球easytreegrid給我一個空的網格樹目錄和文件
function listar_carpeta_archivo($dir = "C:/xampp/htdocs/Archivos") {
$arr = array();
foreach (new DirectoryIterator($dir) as $fileInfo) {
if (!$fileInfo->isDot()) {
$arr[]= $fileInfo->getBasename();
if ($fileInfo->isDir()) {
$arr[] = listar_carpeta_archivo($fileInfo->getPathname());
}
}
}
return $arr;
}
給我這個輸出:
[0] => Array
(
)
[1] => Array
(
[0] => 5962R1121310VXC
[1] => Array
(
[0] => fdgtdfg
[1] => Array
(
[0] => Ingreso
[1] => Array
(
[0] => 2015-08-10 15-13-17
[1] => Array
(
[0] => Nuevo documento de texto - copia (2).txt
[1] => Nuevo documento de texto - copia (3).txt
[2] => Nuevo documento de texto - copia.txt
[3] => Nuevo documento de texto.txt
)
)
)
)
我想這樣的輸出:
[{
"id":1,
"name":"C",
"size":"",
"date":"02/19/2010",
"children":[{
"id":2,
"name":"Program Files",
"size":"120 MB",
"date":"03/20/2010",
"children":[{
"id":21,
"name":"Java",
"size":"",
"date":"01/13/2010",
"state":"closed",
"children":[{
"id":211,
"name":"java.exe",
"size":"142 KB",
"date":"01/13/2010"
},{
"id":212,
"name":"jawt.dll",
"size":"5 KB",
"date":"01/13/2010"
}]
},{
"id":22,
"name":"MySQL",
"size":"",
"date":"01/13/2010",
"state":"closed",
"children":[{
"id":221,
"name":"my.ini",
"size":"10 KB",
"date":"02/26/2009"
},{
"id":222,
"name":"my-huge.ini",
"size":"5 KB",
"date":"02/26/2009"
},{
"id":223,
"name":"my-large.ini",
"size":"5 KB",
"date":"02/26/2009"
}]
}]
},{
"id":3,
"name":"eclipse",
"size":"",
"date":"01/20/2010",
"children":[{
"id":31,
"name":"eclipse.exe",
"size":"56 KB",
"date":"05/19/2009"
},{
"id":32,
"name":"eclipse.ini",
"size":"1 KB",
"date":"04/20/2010"
},{
"id":33,
"name":"notice.html",
"size":"7 KB",
"date":"03/17/2005"
}]
}]
}]的函數
首先,你需要什麼:檢查'$ fileInfo-> getBasename()'不爲空。如果'$ fileInfo-> getBasename()'不爲null,則更改此:if($ fileInfo-> isDir()){arr [] = listar_carpeta_archivo($ fileInfo-> getPathname()); ($ fileInfo-> isDir()&& $ fileInfo-> getPathname()!= null){} {arr [] = listar_carpeta_archivo($ fileInfo-> getPathname()); }' –
**相同的結果@ DanilaGanchar ** – Guillermo
因爲您編輯了帖子,所以結果相同。我還沒有看到**輸出**的例子。嘗試在答案中的功能。 –