如何使用文件夹ID检索数组中的目录结构 [英] How to retrive a directory structure in an array with folder id
问题描述
我想从特定路径文件夹ID和子文件夹parent_id中获取文件夹和子文件夹。
这里我的函数获取文件夹和子文件夹,但没有id和父id。
我想使用id和父id作为数组。
function getDirectory($ path ='。',$ level = 1){
$ result = array();
$ ignore = array('nbproject','src','。','..');
$ dh = @opendir($ path);
$ i = 0;
while($ file = readdir($ dh)){
if(!in_array($ file,$ ignore)){
if(is_dir($ path。'/'。$ file )){
$ level ++;
$ singleResult = array('title'=> $ file,'isFolder'=> true,'children'=> getDirectory($ path。'/'。$ file,$ level),'key '=>'node'。$ level);
$ result [] = $ singleResult;
}
}
$ i ++;
}
closedir($ dh);
返回$ result;
}
$ dir =../UserUpload/Documents/source;
$ kevin = getDirectory($ dir);
这个函数给了我这样的数组wiithout id和父id
array(size = 3)
0 =>
array(size = 4)
'title'=>字符串'mst146'(长度= 6)
'isFolder'=>布尔值true
'children'=>
array(size = 3)
0 =>
array(size = 4)
...
1 =>
array(size = 4)
...
2 =>
array(size = 4)
...
'key'=>字符串'node2'(长度= 5)
1 =>
array(size = 4)
'title'=>字符串't124'(长度= 4)
'isFolder'=>布尔值true
'children'=>
array(size = 0)
empty
'key'=>字符串'node3'(长度= 5)
2 =>
array(size = 4)
'title'=>字符串'test'(长度= 4)
'isFolder'=>布尔值true
'children'=>
array(size = 0)
empty
'key'=>字符串'node4'(长度= 5)
建议您在单独的类中使用scandir函数(用于更清晰的递归)。
class DirectoryScanner {
public $ scannedData;
protected $ ignored = array('nbproject','src','。','..');
public function scanDir($ path){
$ filesAndDirs = scandir($ path);
foreach($ filesAndDirs as $ key => $ dirOrFile){
if(!in_array($ dirOrFile,$ this-> ignored)&& is_dir($ path。DIRECTORY_SEPARATOR。$ dirOrFile )){
$ this-> scannedData [$ path] [$ key] = $ dirOrFile;
$ this-> scanDir($ path。DIRECTORY_SEPARATOR。$ dirOrFile);
$ / code>
它将输出二维数组,其中第一维的键是您找到目录的路径,第二维是目录的顺序(忽略。& ..和文件将导致索引中的偏移),值是目录的名称。 / b>
例如:
数组
(
[/var/www/cluster/private/..../2016-03/] => Array
(
[21] => chity
[25] => ; export-porovnani
[26] => exporty
)
[/var/www/cluster/private/..../2016-03/export-porovnani ] =>数组
(
[13] => vsechno
)
)
您可以通过访问$ scannedData属性从类中获取数据。更好的办法是创建geter并将$ scannedData的可用性设置为protected / private
希望我帮了你一些忙。
I want to fetch folder and sub folder from specific path folder id and sub_folder parent_id. Here my function fetch folder and sub folder but without id and parent id.
I want to array with id and parent id.
function getDirectory($path = '.', $level = 1) {
$result = array();
$ignore = array('nbproject', 'src', '.', '..');
$dh = @opendir($path);
$i = 0;
while ($file = readdir($dh)) {
if (!in_array($file, $ignore)) {
if (is_dir($path . '/' . $file)) {
$level++;
$singleResult = array('title' => $file, 'isFolder' => true, 'children' => getDirectory($path . '/' . $file, $level), 'key' => 'node' . $level);
$result[] = $singleResult;
}
}
$i++;
}
closedir($dh);
return $result;
}
$dir = "../UserUpload/Documents/source";
$kevin = getDirectory($dir);
This function give me array like this wiithout id and parent id
array (size=3)
0 =>
array (size=4)
'title' => string 'mst146' (length=6)
'isFolder' => boolean true
'children' =>
array (size=3)
0 =>
array (size=4)
...
1 =>
array (size=4)
...
2 =>
array (size=4)
...
'key' => string 'node2' (length=5)
1 =>
array (size=4)
'title' => string 't124' (length=4)
'isFolder' => boolean true
'children' =>
array (size=0)
empty
'key' => string 'node3' (length=5)
2 =>
array (size=4)
'title' => string 'test' (length=4)
'isFolder' => boolean true
'children' =>
array (size=0)
empty
'key' => string 'node4' (length=5)
i would suggest you use scandir function inside separate class (for clearer recursion).
class DirectoryScanner{
public $scannedData;
protected $ignored = array('nbproject', 'src', '.', '..');
public function scanDir($path){
$filesAndDirs = scandir($path);
foreach($filesAndDirs as $key => $dirOrFile){
if(!in_array($dirOrFile, $this->ignored) && is_dir($path . DIRECTORY_SEPARATOR . $dirOrFile)){
$this->scannedData[$path][$key] = $dirOrFile;
$this->scanDir($path . DIRECTORY_SEPARATOR . $dirOrFile);
}
}
}
}
it will output two dimensional array, where keys of first dimension is path to where you find directory, second dimension is order of the directory (ignored .&.. and files will cause offset in indexes) and value is name of the directory.
for example:
Array
(
[/var/www/cluster/private/..../2016-03/] => Array
(
[21] => chity
[25] => export-porovnani
[26] => exporty
)
[/var/www/cluster/private/..../2016-03/export-porovnani] => Array
(
[13] => vsechno
)
)
you can get data from class by accessing $scannedData property. better would be to create geter and set availability of $scannedData to protected/private
Hope i helped you a bit.
这篇关于如何使用文件夹ID检索数组中的目录结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!