2010-02-23 125 views
0

我需要按字母順序(在所有級別)下面的數組,但asort似乎不工作,因爲我在我的函數(或者我認爲)遞歸調用;它只對我的數組進行部分排序,所以我會按字母順序排列它,但它們會失序。使用遞歸函數調用對數組進行排序?

幫助!

Ex。目錄列表:

apartments.html 
js/ 
    application.js 
    jquery.js 
    something.js 
css/ 
    reset.css 
    master.css 
preview.html 
ab_restaurant_at_the_end_of_the_universe.jpg 

所需的輸出:

array() { 
    [0] => string() "ab_restaurant_at_the_end_of_the_universe.jpg" 
    [1] => string() "apartments.html" 
    ["css"] => array() { 
    [0] => string() "master.css" 
    [1] => string() "reset.css" 
    } 
    ["js"] => array() { 
    [0] => string() "application.js" 
    [1] => string() "jquery.js" 
    [2] => string() "something.js" 
    } 
    [2] => string() "preview.html" 
} 

function directory_list($directory) { 
    $files = array(); 
    if (is_dir($directory)) { 
     if ($curr_dir = opendir($directory)) { 
     while (false !== ($file = readdir($curr_dir))) { 
     if ($file != "." && $file != ".." && $file != "apartment" && $file != "blog") { 
     if (is_dir($directory. "/" . $file)) { 
      $files[$file] = directory_list($directory. "/" . $file); 
     } else { 
     $files[] = $file; 
     } 
     } 
     } 
     closedir($curr_dir); 
    } 
    } 
    //asort($files); <-- doesn't work; sorts, but interrupts itself because of self-referencing call above (I think) 
    return $files; 
    } 
+0

您是否可以更新您的問題以包含示例目錄列表和預期輸出? – hobodave 2010-02-23 17:44:11

+0

-1不提供請求的信息。 – hobodave 2010-02-23 18:12:03

+0

添加示例輸出。 – neezer 2010-02-23 18:24:36

回答

1

如果更改$files[] = $file;$files[$file] = $file;,那麼你可以使用kso​​rt(),您嘗試使用ASORT()

+0

謝謝!這很好用! – neezer 2010-02-24 21:59:27