任何人都可以指导我完成:此功能通常会读取整个文件夹架构。并显示为数组,但子文件夹也创建数组。但我不需要每个子文件夹的数组只是我需要路径。任何人都可以指导我。
function dirToArray($dir) {
$result = array();
//$result1 = array();
$dummy = array();
$cdir = scandir($dir);
foreach ($cdir as $key => $value)
{
if (!in_array($value,array(".","..")))
{
if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
{
$result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
}
else
{
$result[] = $dir . DIRECTORY_SEPARATOR . $value;
}
}
}
return $result;
}
以上功能正常,但此功能需要小修改。
$dir = "test";
echo "<pre>";
print_r(dirToArray($dir));
输出:
Array
(
[admin] => Array
(
[0] => test\admin\asdf1.txt
)
[app] => Array
(
[0] => test\app\asdf1.txt
[1] => test\app\two.txt
)
[0] => test\asdf1.txt
)
预期输出为:
Array
(
[0] => test\admin\asdf1.txt
[1] => test\app\asdf1.txt
[2] => test\app\two.txt
[3] => test\asdf1.txt
)
答案 0 :(得分:0)
您可以展平阵列。根据{{3}},您可以使用以下函数将其展平:
function arrayFlatten($array) {
$flattern = array();
foreach ($array as $key => $value){
$new_key = array_keys($value);
$flattern[] = $value[$new_key[0]];
}
return $flattern;
}
$flattened = arrayFlatten( $dir );