如何在 php 中对多键进行排序?
How to sort multikey in php?
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 ) )
这是我的数组输入 Like
Id Date Title Description
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
我想要像
这样的输出
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
如何在 php 中对多键进行排序?
我是 Php 的初学者。我在 php 中使用以下代码,但输出不正确。如果按日期或按月排序中的任何一种,输出都会正确。否则两者(按日期或按月),输出都不会正确。请帮助任何解决方案。
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;
});
下面的代码可以完成这项工作。 strtotime 将文本日期解析为 Unix 时间戳。
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);
});
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 ) )
这是我的数组输入 Like
Id Date Title Description
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
我想要像
这样的输出 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
如何在 php 中对多键进行排序? 我是 Php 的初学者。我在 php 中使用以下代码,但输出不正确。如果按日期或按月排序中的任何一种,输出都会正确。否则两者(按日期或按月),输出都不会正确。请帮助任何解决方案。
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;
});
下面的代码可以完成这项工作。 strtotime 将文本日期解析为 Unix 时间戳。
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);
});