使用php在多维数组中按键排序



可能重复:
PHP 中的多维数组排序

如何在多维数组中按关键字排序?

例如,下面是我从数据库打印的数组,其中最新的排在第一位-12月、11月、10月等2011年、2010年、2009年等

Array
(
    [0] => Array
        (
            [URL] => september 2011
            [Title] => September 2011
            [Date] => 8
            [Month] => 9
            [Year] => 2011
        )
    [1] => Array
        (
            [URL] => january 2011
            [Title] => January 2011
            [Date] => 1
            [Month] => 2
            [Year] => 2011
        )
    [2] => Array
        (
            [URL] => february 2011
            [Title] => February 2011
            [Date] => 4
            [Month] => 1
            [Year] => 2011
        )
    [3] => Array
        (
            [URL] => november 2011
            [Title] => November 2011
            [Date] => 23
            [Month] => 11
            [Year] => 2010
        )
    [4] => Array
        (
            [URL] => april 2011
            [Title] => April 2011
            [Date] => 23
            [Month] => 4
            [Year] => 2010
        )
)

但我需要它是这样的,10月、11月、12月等2011年、2010年、2009年等-请注意,月份按最早的排在第一位,但年份仍按最新的排在第一。

所以数组应该像这样排序,

Array
(
    [2] => Array
        (
            [URL] => february 2011
            [Title] => February 2011
            [Date] => 4
            [Month] => 1
            [Year] => 2011
        )
    [1] => Array
        (
            [URL] => january 2011
            [Title] => January 2011
            [Date] => 1
            [Month] => 2
            [Year] => 2011
        )
    [0] => Array
        (
            [URL] => september 2011
            [Title] => September 2011
            [Date] => 8
            [Month] => 9
            [Year] => 2011
        )
    [4] => Array
        (
            [URL] => april 2010
            [Title] => April 2010
            [Date] => 23
            [Month] => 4
            [Year] => 2010
        )
    [3] => Array
        (
            [URL] => november 2010
            [Title] => November 2010
            [Date] => 23
            [Month] => 11
            [Year] => 2010
        )
)

这可能吗?

用多个键对数组进行排序的通用解决方案

根据我对这个问题的回答,这里有一个非常通用的解决方案,你可以在很多情况下使用。

限制:由于存在匿名函数,需要PHP>=5.3才能工作。

新的和改进的,现在支持降序排序

function make_comparer() {
    $criteriaNames = func_get_args();
    $comparer = function($first, $second) use ($criteriaNames) {
        // Do we have anything to compare?
        while(!empty($criteriaNames)) {
            // What will we compare now?
            $criterion = array_shift($criteriaNames);
            // Used to reverse the sort order by multiplying
            // 1 = ascending, -1 = descending
            $sortOrder = 1; 
            if (is_array($criterion)) {
                $sortOrder = $criterion[1] == SORT_DESC ? -1 : 1;
                $criterion = $criterion[0];
            }
            // Do the actual comparison
            if ($first[$criterion] < $second[$criterion]) {
                return -1 * $sortOrder;
            }
            else if ($first[$criterion] > $second[$criterion]) {
                return 1 * $sortOrder;
            }
        }
        // Nothing more to compare with, so $first == $second
        return 0;
    };
    return $comparer;
}

如何使用

按年份升序排序:

uasort($array, make_comparer('Year'));

按年份升序排序,然后按月份升序排序:

uasort($array, make_comparer('Year', 'Month'));

按年份降序排序,然后按月份升序排序:

uasort($array, make_comparer(array('Year', SORT_DESC), 'Month'));

最后一个就是你想要的。

假设您在问题中提供的数据实际上是正确的(以年、月、日期为基础创建排序(,您可以首先根据这些值对数组进行索引,然后对主数组进行排序。我只是在写它,否则你可能会误读Demo的输出,URL/Titles与给定的数值不对应,但它只是起作用:

// create an index by date
foreach($data as $k=>$v)
{
    $index[$k] = sprintf('%04d-%02d-%02d', $v['Year'], $v['Month'], $v['Date']);
}
// sort data based on the index
array_multisort($index, SORT_DESC, $data);

怎么样:

数据:

$arr = Array (
    Array (
        'URL' => 'september 2011',
        'Title' => 'September 2011',
        'Date' => 8,
        'Month' => 9,
        'Year' => 2011,
    ),
    Array (
        'URL' => 'january 2011',
        'Title' => 'January 2011',
        'Date' => 1,
        'Month' => 2,
        'Year' => 2011,
    ),
    Array (
        'URL' => 'february 2011',
        'Title' => 'February 2011',
        'Date' => 4,
        'Month' => 1,
        'Year' => 2011,
    ),
    Array (
        'URL' => 'november 2011',
        'Title' => 'November 2011',
        'Date' => 23,
        'Month' => 11,
        'Year' => 2010,
    ),
    Array (
        'URL' => 'april 2011',
        'Title' => 'April 2011',
        'Date' => 23,
        'Month' => 4,
        'Year' => 2010,
    ),
);

代码:

function compare($a, $b) {
    if ($a['Year'] == $b['Year']) {
      return ($a['Month'] - $b['Month']);
    } else {
        return ($b['Year'] - $a['Year']);
    }
}    
usort($arr, 'compare');
print_r($arr);    

输出:

Array
(
[0] => Array
    (
        [URL] => february 2011
        [Title] => February 2011
        [Date] => 4
        [Month] => 1
        [Year] => 2011
    )
[1] => Array
    (
        [URL] => january 2011
        [Title] => January 2011
        [Date] => 1
        [Month] => 2
        [Year] => 2011
    )
[2] => Array
    (
        [URL] => september 2011
        [Title] => September 2011
        [Date] => 8
        [Month] => 9
        [Year] => 2011
    )
[3] => Array
    (
        [URL] => april 2011
        [Title] => April 2011
        [Date] => 23
        [Month] => 4
        [Year] => 2010
    )
[4] => Array
    (
        [URL] => november 2011
        [Title] => November 2011
        [Date] => 23
        [Month] => 11
        [Year] => 2010
    )
)

相关内容

  • 没有找到相关文章

最新更新