如何在php中对多键进行排序?
这是我的数组输入如何在php中对多键进行排序?,php,arrays,sorting,quicksort,spl,Php,Arrays,Sorting,Quicksort,Spl,这是我的数组输入 Array ( [0] => stdClass Object ( [Id] => 18 [AccNo] => 1 [Title] => Hardware [Description] => Mobile.
Array ( [0] => stdClass Object ( [Id] => 18
[AccNo] => 1
[Title] => Hardware
[Description] => Mobile.
[ManuDate] => 8th July 1942
[MusCat] => Album
[month] => 7
[date] => 8 )
[1] => stdClass Object ( [Id] => 20
[AccNo] => 2
[Title] => Food
[Description] => Apple.
[ManuDate] => 27th July 1942
[MusCat] => Album
[month] => 7
[date] => 27 )
[2] => stdClass Object ( [Id] => 24
[AccNo] => 3
[Title] => Hardware
[Description] => Computer.
[ManuDate] => 2nd July 1942
[MusCat] => Album
[month] => 7
[date] => 2 )
[3] => stdClass Object ( [Id] => 56
[AccNo] => 4
[Title] => Hardware
[Description] => Printer
[ManuDate] => 1942
[MusCat] => Album
[month] =>
[date] => 0 )
[4] => stdClass Object ( [Id] => 105
[AccNo] => 5
[Title] => Object
[Description] => Chair.
[ManuDate] => 1942
[MusCat] => Album
[month] =>
[date] => 0 ) )
我想要像这样的输出
0 8th July 1942 Hardware Mobile
1 27th August 1942 Food Apple
2 2nd July 1942 Hardware Computer
3 1942 Hardware Printer
4 1942 Object Chair
如何在php中对多键进行排序?
我是Php的初学者。我在php中使用了以下代码,但Output将无法正确执行。如果是datewise或monthwise排序,输出将正确。否则(按日期或按月份),输出将不正确。请帮助任何解决方案
Id Date Title Description
************************************************************
3 1942 Hardware Printer
4 1942 Object Chair
2 2nd July 1942 Hardware Computer
0 8th July 1942 Hardware Mobile
1 27th August 1942 Food Apple
usort($value['year'],函数($a,$b){
如果($a->date==$b->date)返回0;
返回$a->date<$b->date?-1:1;
});
usort($value['year'],函数($a,$b){
如果($a->month==$b->month)返回0;
返回$a->month<$b->month?-1:1;
});
以下代码执行此操作。将文本日期解析为Unix时间戳
usort($value['year'], function ($a, $b) {
if ($a->date == $b->date) return 0;
return $a->date < $b->date ? -1 : 1;
});
usort($value['year'], function ($a, $b) {
if ($a->month == $b->month) return 0;
return $a->month < $b->month ? -1 : 1;
});
您可以简单地在MySQL中使用
order by
子句,而不是在PHP中对其进行排序QueryEAR看起来不像DB中的日期字段,在MySQL中将其设置为日期字段,然后再设置为order by
?如果您仍想在php中执行此操作,则year不是有效的datetime,因此您需要检查此字段是否为year并将其转换为date,然后compare.date应具有year或date。我得分月分日。无法在QueryTanks中按日期排序以供参考。我找到了解决办法。我将manudate转换为日期格式,如1942-07-02或1942-01-01或1942-08-27。我添加了tempyear键属性并在函数date\u compare($a,$b){$t1=strottime($a->tempyear);$t2=strottime($b->tempyear);返回$t1-$t2;}usort(#数组,'date\u compare');
usort($value['year'], function ($a, $b) {
if ($a->date == $b->date) return 0;
return $a->date < $b->date ? -1 : 1;
});
usort($value['year'], function ($a, $b) {
if ($a->month == $b->month) return 0;
return $a->month < $b->month ? -1 : 1;
});
usort($array, function($v1, $v2) {
$d1 = strlen($v1->ManuDate) === 4 ? '01-01-' . $v1->ManuDate : $v1->ManuDate;
$d2 = strlen($v2->ManuDate) === 4 ? '01-01-' . $v2->ManuDate : $v2->ManuDate;
return strtotime($d1) - strtotime($d2);
});