[英]scandir recursivly with php
I have a specific question. 我有一个具体的问题。 I'm new on php so it still gets a bit tough for me :)
我是php新手,所以对我来说仍然有点困难:)
So, I have a function which shoud scan dirs recursivly and print a specific array from result. 所以,我有一个功能,应该递归扫描目录并从结果中打印出一个特定的数组。 I almost understand why it is happening, but I could not figure out how to change it like I need to.
我几乎理解为什么会发生这种情况,但是我无法弄清楚如何根据需要进行更改。 Please help!
请帮忙!
At the moment I'm getting an array: 目前,我得到一个数组:
Array
(
[0] => Array
(
[album1] => Array
(
[0] => .
[1] => ..
)
)
[1] => Array
(
[album2] => Array
(
[0] => .
[1] => ..
)
)
[2] => Array
(
[album3] => Array
(
[0] => .
[1] => ..
)
)
)
But I need to get: 但是我需要得到:
Array
(
[album1] => Array (
[0] => .
[1] => ..
)
[album2] => Array
(
[0] => .
[1] => ..
)
[album3] => Array
(
[0] => .
[1] => ..
)
)
The function itself is: 该函数本身是:
<?php
function RAgetFiles($main_dir, $result = array()) {
$dirs = scandir($main_dir);
foreach($dirs as $dir) {
if (is_dir("$main_dir/$dir")){
if ($dir === '.' || $dir === '..') {
continue; }
$files=scandir($main_dir."/".$dir);
$result[] = array($dir => $files);
}
}
return $result;
}
$rafiles = RAgetFiles('thumbs');
echo '<pre>';
print_r($rafiles);
echo '<pre>';
?>
<?php
function RAgetFiles($main_dir, $result = array()) {
$dirs = scandir($main_dir);
foreach($dirs as $dir) {
if (is_dir("$main_dir/$dir")){
if ($dir === '.' || $dir === '..') {
continue; }
$files=scandir($main_dir."/".$dir);
$result[$dir] = $files;
}
}
return $result;
}
$rafiles = RAgetFiles('/usr');
echo '<pre>';
print_r($rafiles);
echo '</pre>';
?>
Works perfectly. 完美运作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.