如何在多维数组中按键排序?
例如,下面是我从我的数据库打印的数组,其中最新首先出现 - 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
)
)
这可能吗?
答案 0 :(得分:3)
根据我对this question的回答,这是一个非常通用的解决方案,您可以在很多情况下使用。
限制:由于存在匿名函数,需要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'));
最后一个就是你要追求的目标。
答案 1 :(得分:0)
假设您在问题中提供的数据实际上是正确的(以Year,Month,Date为基础来创建排序),您可以先按这些值索引数组并对主数组进行排序。我只是写它,否则你可能会误读Demo的输出,URL /标题与给定的数值不对应,但它只是起作用:
// 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);
答案 2 :(得分:0)
怎么样:
数据:
$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
)
)