我想构建一个Json输出来列出ExtJS树面板中的文件夹结构。结构应该等同于Json中的这个数组:
Array
(
[text] => .
[children] => Array
(
[0] => Array
(
[text] => files
[children] =>
)
[1] => Array
(
[text] => folder 1
[children] =>
)
[2] => Array
(
[text] => New directory
[children] => array(
[0] => Array
(
[text] => sub_1
[children] => array(
[0] => Array
(
[text] => sub_1_1
[children] =>
)
[1] => Array
(
[text] => sub_1_2
[children] =>
)
)
)
[1] => Array
(
[text] => sub_2
[children] =>
)
)
)
)
)
我做了这个功能,通过PHP手册和例子
显示结构listFolders('../file_uploads/');
function listFolders($dir){
$dh = scandir($dir);
echo '<ul>';
foreach($dh as $folder){
if($folder != '.' && $folder != '..')
{
if(is_dir($dir.'/'.$folder)){
echo '<li>'.$folder.'</li>';
listFolders($dir.'/'.$folder);
}
}
}
echo '</ul>';
}
这输出结构
我想知道如何将此输出转换为上面的数组(或Json)?
溶液
print "<pre>";
print_r(listFolders('../file_uploads/'));
function listFolders($dir)
{
$dh = scandir($dir);
$return = array();
foreach ($dh as $folder) {
if ($folder != '.' && $folder != '..') {
if (is_dir($dir . '/' . $folder)) {
$return[] = array(
'text' => $folder,
'children' => listFolders($dir . '/' . $folder)
);
}
}
}
return $return;
}
答案 0 :(得分:6)
请尝试以下代码:
function listFolders($dir)
{
$dh = scandir($dir);
$return = array();
foreach ($dh as $folder) {
if ($folder != '.' && $folder != '..') {
if (is_dir($dir . '/' . $folder)) {
$return[] = array($folder => listFolders($dir . '/' . $folder));
} else {
$return[] = $folder;
}
}
}
return $return;
}
似乎对我有用,但未经过适当的测试。
答案 1 :(得分:1)
首先创建一个数组。对于每个创建的目录,使用array_push到数组?
答案 2 :(得分:1)
<?php
error_reporting(0);
$array_folders = listFolders('../file_uploads/');
echo "<pre>";
print_r($array_folders);
function listFolders($dir){
global $ret_array;
$dh = scandir($dir);
foreach($dh as $folder){
if($folder != '.' && $folder != '..')
{
if(is_dir($dir.'/'.$folder)){
$ret_array[$dir][] = $folder;
listFolders($dir.'/'.$folder);
}
}
}
return $ret_array;
}
?>
答案 3 :(得分:0)
我认为您正在寻找递归目录列表。使用下面的代码我希望它可以帮助你。它对我有用。
<?php
/**
* Function for recursive directory file list search as an array.
*
* @param mixed $dir Main Directory Path.
*
* @return array
*/
function listFolderFiles($dir)
{
$fileInfo = scandir($dir);
$allFileLists = [];
foreach ($fileInfo as $folder) {
if ($folder !== '.' && $folder !== '..') {
if (is_dir($dir . DIRECTORY_SEPARATOR . $folder) === true) {
$allFileLists[$folder . '/'] = listFolderFiles($dir . DIRECTORY_SEPARATOR . $folder);
} else {
$allFileLists[$folder] = $folder;
}
}
}
return $allFileLists;
}//end listFolderFiles()
$dir = listFolderFiles('your searching directory path ex:-F:\xampp\htdocs\abc');
echo '<pre>';
print_r($dir);
echo '</pre>'
?>
答案 4 :(得分:0)
要获得完全相同的结果,请遵循以下代码
function listFolderFiles($dir){
$ffs = scandir($dir);
unset($ffs[array_search('.', $ffs, true)]);
unset($ffs[array_search('..', $ffs, true)]);
// prevent empty ordered elements
//if (count($ffs) < 1)
// return;
$dirStrArr = array();
foreach($ffs as $ff){
if(is_dir($dir.'/'.$ff)) {
$dirStrArr[]=array('parent' => $ff, 'children' => listFolderFiles($dir . '/' . $ff));
}else{
$dirStrArr[]=$ff;
}
}
return $dirStrArr;
}
或者要将文件夹名称作为索引以及索引数组中的子详细信息,您可以按照下面的代码
function listFolderFiles($dir){
$ffs = scandir($dir);
unset($ffs[array_search('.', $ffs, true)]);
unset($ffs[array_search('..', $ffs, true)]);
// prevent empty ordered elements
//if (count($ffs) < 1)
// return;
$dirStrArr = array();
foreach($ffs as $ff){
if(is_dir($dir.'/'.$ff)) {
$dirStrArr[$ff]=listFolderFiles($dir . '/' . $ff);
}else{
$dirStrArr[]=$ff;
}
}
return $dirStrArr;
}