【问题标题】:How to sort an array created by the directory_helper in CodeIgniter如何对 CodeIgniter 中的 directory_helper 创建的数组进行排序
【发布时间】:2013-04-20 12:23:48
【问题描述】:

所以我在 Directory Helper 中使用 directory_map 函数,我想知道如何编辑该函数(或者扩展它或其他什么),以便对它给我的多维数组进行排序。

这是它当前产生的数组;

 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() 的尝试都会导致错误。这就是为什么我认为我可能必须编辑这个函数或者为我创建一个新函数......

【问题讨论】:

  • 我不认为asort可以对多维数组进行排序。你有没有使用array_multisort()???

标签: arrays codeigniter sorting helpers


【解决方案1】:

您必须拆分数组,分别对文件和目录进行排序,因为目录名在键中,文件名在值中。

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

/**
 * 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;
}

【讨论】:

    【解决方案2】:

    您可以在 SQL 选择语句 (ORDER BY XXX) 或在控制器中使用以下方法之一进行排序

    [1]:http://php.net/manual/en/array.sorting.phpphp.net

    【讨论】:

      猜你喜欢
      • 2021-10-11
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 2016-02-26
      • 2010-10-12
      相关资源
      最近更新 更多