Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从数组中获取完整的文件路径_Php_Arrays_File_Recursion_Directory - Fatal编程技术网

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
函数,可能会有所帮助。