从多维数组中查找文件路径

时间:2011-09-10 04:36:25

标签: php arrays find filepath multidimensional-array

我下面有目录数组。

我将在此数组中进行文件搜索。例如path.php,survey.php ...... 如果找到文件,应该如何构造路径。

for path.php

我希望函数返回'/survey/config/path.php'

Array
(
[survey] => Array
    (
        [config] => Array
            (
                [0] => path.php
                [1] => routes.php
            )

        [controllers] => Array
            (
                [0] => admin.php
                [1] => giris.php
            )

        [models] => Array
            (
                [0] => giris.php
            )

        [views] => Array
            (
                [0] => admin_form.php
                [1] => widget.php
                [2] => yeni_form.php
            )

        [widgets] => Array
            (
                [0] => survey.php
            )

    )

1 个答案:

答案 0 :(得分:1)

function find_file_path($dir_structure, $filename) {
    foreach($dir_structure as $dir => $subpath) {
        if(is_array($subpath)) {
            $sub_found = find_file_path($subpath, $filename);
            if($sub_found) {
                return "/" . $dir . $sub_found;
            }
        } else {
            if($subpath === $filename) {
                return "/$filename";
            }
        }
    }
    return FALSE;
}