2011-09-10 50 views
0

我得到了下面的目錄數組。從多維數組中找到文件路徑

我將在這個數組中進行文件搜索。例如path.php,survey.php ... 如果找到文件,應該如何構建路徑。

爲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
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; 
} 
+0

感謝。但它返回survey/config/0/path.php。我如何刪除「0」或「1」? just survey/config/path.php –

+0

非常感謝你:) –

+0

如果答案解決了你的問題,你應該點擊旁邊的綠色複選標記來標記爲「已接受」。 :) – Amber