Php 从数组中获取完整的文件路径
我有一个数组(它来自一个特定的函数:列出和过滤扩展名上目录的文件): 我想得到类似的东西。注意:目录可能有更多的子目录Php 从数组中获取完整的文件路径,php,arrays,file,recursion,directory,Php,Arrays,File,Recursion,Directory,我有一个数组(它来自一个特定的函数:列出和过滤扩展名上目录的文件): 我想得到类似的东西。注意:目录可能有更多的子目录 Array ( [0] = Dir_test/dir_client/index.html [1] = Dir_test/index.html ) 谢谢你的帮助;) 您可以使用function您可以使用function编写一个递归函数,该函数将数组和字符串作为参数,并在数组中保存自身数组的每个键上返回和调用自身。编写一个递归函数,该函数将数组和字符串作为参数,并
Array
(
[0] = Dir_test/dir_client/index.html
[1] = Dir_test/index.html
)
谢谢你的帮助;) 您可以使用function您可以使用function编写一个递归函数,该函数将数组和字符串作为参数,并在数组中保存自身数组的每个键上返回和调用自身。编写一个递归函数,该函数将数组和字符串作为参数,并在数组的每个键上返回和调用自身保存自身为数组的数组。假设您的输入数据如下所示:
$arr = array(
'Dir_test' => array (
'dir_client' => array (
0 => 'index.html'
),
0 => 'index.html'
)
);
最简单的解决方案是递归解决方案,类似于:
function add_dir($dir) {
global $dirs;
$dirs[] = $dir;
}
// pathsofar should always end in '/'
function process_dir($dirarray, $pathsofar) {
foreach ($dirarray as $key => $value) {
if (is_array($value)) {
process_dir($value, $pathsofar . $key . '/');
} else {
add_dir($pathsofar . $value);
}
}
}
process_dir($arr, '');
print_r($dirs);
运行它:
$ php arr2.php
Array
(
[0] => Dir_test/dir_client/index.html
[1] => Dir_test/index.html
)
假设您的输入数据如下所示:
$arr = array(
'Dir_test' => array (
'dir_client' => array (
0 => 'index.html'
),
0 => 'index.html'
)
);
最简单的解决方案是递归解决方案,类似于:
function add_dir($dir) {
global $dirs;
$dirs[] = $dir;
}
// pathsofar should always end in '/'
function process_dir($dirarray, $pathsofar) {
foreach ($dirarray as $key => $value) {
if (is_array($value)) {
process_dir($value, $pathsofar . $key . '/');
} else {
add_dir($pathsofar . $value);
}
}
}
process_dir($arr, '');
print_r($dirs);
运行它:
$ php arr2.php
Array
(
[0] => Dir_test/dir_client/index.html
[1] => Dir_test/index.html
)
PHP有一个
is_array
函数,可能会有所帮助。PHP有一个is_array
函数,可能会有所帮助。