2013-04-26 4 views
1

그래서 Directory Helper에서 directory_map 함수를 사용하고 있는데이 함수를 편집하거나 (확장하거나 무언가를 확장하여) 정렬 할 수 있는지 궁금합니다. 그것을 제공하는 다차원 배열.CodeIgniter에서 directory_helper로 작성한 배열을 정렬하는 방법

현재 생성 된 배열입니다.

Array 
(
    [publications] => Array 
    (
     [policy_documents] => Array 
      (
       [_careers] => Array 
        (
         [0] => careers.pdf 
        ) 
       [_background_quality_reports] => Array 
        (
         [0] => industry.pdf 
         [1] => international.pdf 
         [2] => departmental_resources.pdf 
         [3] => contracts.pdf 
         [4] => research_and_development.pdf 
         [5] => trade.pdf 
        ) 
       [_pre_release_access_list] => Array 
        (
         [0] => pre_release_access_list.pdf 
        ) 
      ) 
     [people] => Array 
      (
       [health] => Array 
        (
         [very_serious_injuries] => Array 
          (
           [_1_january_2013] => Array 
            (
             [0] => 1_january_2013.pdf 
            ) 
          ) 
        ) 
       [military] => Array 
        (
         [quarterly_manning_report] => Array 
          (
           [_1_january_2013] => Array 
            (
             [0] => 1_january_2013.pdf 
            ) 
          ) 
         [monthly_manning_report] => Array 
          (
           [_20110201_1_february_2011] => Array 
            (
             [0] => 1_february_2011.xls 
             [1] => key_points.html 
             [2] => 1_february_2011.pdf 
            ) 
           [_20110301_1_march_2011] => Array 
            (
             [0] => 1 March 2011.pdf 
            ) 
           [_20110501_1_may_2011] => Array 
            (
             [0] => 1 May 2011.pdf 
            ) 
           [_20110401_1_april_2011] => Array 
            (
             [0] => 1 April 2011.pdf 
            ) 
          ) 
        ) 
       [civilian] => Array 
        (
         [civilian_personnel_report] => Array 
          (
           [_1_april_2012] => Array 
            (
             [0] => 1_april_2012.pdf 
            ) 
           [_1_october_2012] => Array 
            (
             [0] => 1_october_2012.pdf 
            ) 
           [_1_january_2013] => Array 
            (
             [0] => 1_january_2013.pdf 
             [1] => key_points.html 
            ) 
           [_1_july_2012] => Array 
            (
             [0] => 1_july 2012.pdf 
            ) 
          ) 
        ) 
       [search_and_rescue] => Array 
        (
         [monthly] => Array 
          (
           [_1_February_2013] => Array 
            (
             [0] => 1_february_2013.pdf 
            ) 
          ) 
         [annual] => Array 
          (
           [_2012] => Array 
            (
             [0] => 2012.pdf 
            ) 
          ) 
         [quarterly] => Array 
          (
           [_q3_2012] => Array 
            (
             [0] => q3_2012.pdf 
            ) 
          ) 
        ) 
      ) 
     [estate] => Array 
      (
      ) 
    ) 
) 

좀 지저분하지만 생각을하게됩니다. 내 모델에서 변수 주위에 sort() 또는 asort()를 래핑하려고 시도하면 오류가 발생합니다. 어느 내가 나에게 새로운 하나를 만들 아마이 기능을 편집하거나해야 할 수 있습니다 생각하는 이유는 ...

+0

난 그렇게 따라 asort 생각하지 않는다 ** 다차원 배열을 정렬 할 수 있습니다 **

$dir_map = dir_map_sort(directory_map('folder/name'));

. U array_multisort()를 사용 했습니까 ??? –

답변

4

디렉토리 이름이 키에 있고 파일 이름이 값에 있으므로 파일과 디렉토리를 개별적으로 정렬해야합니다.

/** 
* Sorts the return of directory_map() alphabetically 
* directories listed before files 
* 
* Example: 
* a_dir/ 
* b_dir/ 
* a_file.dat 
* b_file.dat 
*/ 
function dir_map_sort($array) 
{ 
    $dirs = array(); 
    $files = array(); 

    foreach ($array as $key => $val) 
    { 
     if (is_array($val)) // if is dir 
     { 
      // run dir array through function to sort subdirs and files 
      // unless it's empty 
      $dirs[$key] = (!empty($array)) ? dir_map_sort($val) : $val; 
     } 
     else 
     { 
      $files[$key] = $val; 
     } 
    } 

    ksort($dirs); // sort by key (dir name) 
    asort($files); // sort by value (file name) 

    // put the sorted arrays back together 
    // swap $dirs and $files if you'd rather have files listed first 
    return array_merge($dirs, $files); 
} 

또는

/** 
* Sorts the return of directory_map() alphabetically 
* with directories and files mixed 
* 
* Example: 
* a_dir/ 
* a_file.dat 
* b_dir/ 
* b_file.dat 
*/ 
function dir_map_sort($array) 
{ 
    $items = array(); 

    foreach ($array as $key => $val) 
    { 
     if (is_array($val)) // if is dir 
     { 
      // run dir array through function to sort subdirs and files 
      // unless it's empty 
      $items[$key] = (!empty($array)) ? dir_map_sort($val) : $val; 
     } 
     else 
     { 
      $items[$val] = $val; 
     } 
    } 

    ksort($items); // sort by key 

    return $items; 
}