2010-02-23 124 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