有一个PHP数组,我想按日期和时间对其进行排序,但是事情没有按预期进行。如何根据递增的日期和时间排序此数组?
原始数组:
Array
(
[0] => Array
(
[ID] => 44
[Create_date] => 2018-07-07
[Create_time] => 19:00:56
[Invoice_series] => RR
[Invoice_id] => 9
[Amount] => 9402
[Type] => Debit
[Method] => Sale
)
[1] => Array
(
[ID] => 46
[Create_date] => 2018-07-09
[Create_time] => 16:13:36
[Invoice_series] => RR
[Invoice_id] => 10
[Amount] => 598
[Type] => Debit
[Method] => Sale
)
[2] => Array
(
[ID] => 47
[Create_date] => 2018-07-10
[Create_time] => 16:52:22
[Invoice_series] => VS
[Invoice_id] => 5
[Amount] => 10000
[Type] => Debit
[Method] => Sale
)
[3] => Array
(
[ID] => 48
[Create_date] => 2018-07-10
[Create_time] => 16:59:32
[Invoice_series] => VS
[Invoice_id] => 6
[Amount] => 16757
[Type] => Debit
[Method] => Sale
)
[4] => Array
(
[ID] => 76
[Create_date] => 2018-10-01
[Create_time] => 11:44:20
[Invoice_series] => VS
[Invoice_id] => 14
[Amount] => 1799
[Type] => Debit
[Method] => Sale
)
[5] => Array
(
[ID] => 77
[Create_date] => 2018-10-01
[Create_time] => 18:41:00
[Invoice_series] => VS
[Invoice_id] => 15
[Amount] => 1213
[Type] => Debit
[Method] => Sale
)
[6] => Array
(
[ID] => 78
[Create_date] => 2018-10-01
[Create_time] => 18:44:08
[Invoice_series] => VS
[Invoice_id] => 16
[Amount] => 19656
[Type] => Debit
[Method] => Sale
)
[7] => Array
(
[ID] => 89
[Create_date] => 2018-10-04
[Create_time] => 10:02:50
[Invoice_series] => VS
[Invoice_id] => 17
[Amount] => 10251
[Type] => Debit
[Method] => Sale
)
[8] => Array
(
[ID] => 94
[Create_date] => 2018-10-22
[Create_time] => 14:58:22
[Invoice_series] => VS
[Invoice_id] => 21
[Amount] => 9251
[Type] => Debit
[Method] => Sale
)
[9] => Array
(
[ID] => 97
[Create_date] => 2018-10-24
[Create_time] => 11:47:51
[Invoice_series] => VS
[Invoice_id] => 22
[Amount] => 5000
[Type] => Debit
[Method] => Sale
)
[10] => Array
(
[ID] => 98
[Create_date] => 2018-10-24
[Create_time] => 15:02:05
[Invoice_series] => VS
[Invoice_id] => 23
[Amount] => 5000
[Type] => Debit
[Method] => Sale
)
[11] => Array
(
[ID] => 109
[Create_date] => 2018-10-26
[Create_time] => 18:26:13
[Invoice_series] => VS
[Invoice_id] => 24
[Amount] => 10251
[Type] => Debit
[Method] => Sale
)
[12] => Array
(
[ID] => 9
[Create_date] => 2018-07-28
[Create_time] => 15:10:33
[Invoice_series] => SRT
[Invoice_id] => 1
[Amount] => 1237
[Type] => Credit
[Method] => sale_return
)
[13] => Array
(
[ID] => 10
[Create_date] => 2018-09-17
[Create_time] => 15:43:30
[Invoice_series] => SRT
[Invoice_id] => 2
[Amount] => 361
[Type] => Credit
[Method] => sale_return
)
[14] => Array
(
[ID] => 13
[Create_date] => 2018-09-18
[Create_time] => 10:04:38
[Invoice_series] => SRT
[Invoice_id] => 3
[Amount] => 4307
[Type] => Credit
[Method] => sale_return
)
[15] => Array
(
[ID] => 14
[Create_date] => 2018-09-18
[Create_time] => 10:06:52
[Invoice_series] => SRT
[Invoice_id] => 4
[Amount] => 6262
[Type] => Credit
[Method] => sale_return
)
[16] => Array
(
[ID] => 15
[Create_date] => 2018-09-29
[Create_time] => 17:02:39
[Invoice_series] => SRT
[Invoice_id] => 5
[Amount] => 2253
[Type] => Credit
[Method] => sale_return
)
[17] => Array
(
[ID] => 18
[Create_date] => 2018-10-12
[Create_time] => 11:14:34
[Invoice_series] => SRT
[Invoice_id] => 6
[Amount] => 3186
[Type] => Credit
[Method] => sale_return
)
[18] => Array
(
[ID] => 29
[Create_date] => 2018-10-24
[Create_time] => 19:44:03
[Invoice_series] => SRT
[Invoice_id] => 7
[Amount] => 5000
[Type] => Credit
[Method] => sale_return
)
[19] => Array
(
[ID] => 30
[Create_date] => 2018-10-25
[Create_time] => 16:52:29
[Invoice_series] => SRT
[Invoice_id] => 8
[Amount] => 10251
[Type] => Credit
[Method] => sale_return
)
[20] => Array
(
[ID] => 50
[Create_date] => 2018-10-26
[Create_time] => 12:00:51
[Invoice_series] => SRT
[Invoice_id] => 9
[Amount] => 5000
[Type] => Credit
[Method] => sale_return
)
[21] => Array
(
[ID] => 51
[Create_date] => 2018-10-26
[Create_time] => 15:25:36
[Invoice_series] => SRT
[Invoice_id] => 10
[Amount] => 5000
[Type] => Credit
[Method] => sale_return
)
[22] => Array
(
[ID] => 52
[Create_date] => 2018-10-27
[Create_time] => 18:14:15
[Invoice_series] => SRT
[Invoice_id] => 11
[Amount] => 10251
[Type] => Credit
[Method] => sale_return
)
[23] => Array
(
[ID] => 1018
[Create_date] => 2018-11-23
[Create_time] => 16:08:17
[Payment_series] => RecT
[Payment_id] => 2
[Amount] => -20251
[Type] => Debit
[Method] => payment
)
[24] => Array
(
[ID] => 1020
[Create_date] => 2018-11-23
[Create_time] => 17:30:14
[Payment_series] => RecT
[Payment_id] => 3
[Amount] => 9251
[Type] => Credit
[Method] => payment
)
[25] => Array
(
[ID] => 1021
[Create_date] => 2018-11-23
[Create_time] => 17:30:33
[Payment_series] => RecT
[Payment_id] => 4
[Amount] => 0
[Type] => Debit
[Method] => payment
)
[26] => Array
(
[ID] => 1016
[Create_date] => 2018-11-23
[Create_time] => 11:28:57
[Payment_series] => RecT
[Payment_id] => 1
[Amount] => 20251
[Type] => Credit
[Method] => payment
)
)
USORT数组:
Array
(
[0] => Array
(
[ID] => 44
[Create_date] => 2018-07-07
[Create_time] => 19:00:56
[Invoice_series] => RR
[Invoice_id] => 9
[Amount] => 9402
[Type] => Debit
[Method] => Sale
)
[1] => Array
(
[ID] => 46
[Create_date] => 2018-07-09
[Create_time] => 16:13:36
[Invoice_series] => RR
[Invoice_id] => 10
[Amount] => 598
[Type] => Debit
[Method] => Sale
)
[2] => Array
(
[ID] => 47
[Create_date] => 2018-07-10
[Create_time] => 16:52:22
[Invoice_series] => VS
[Invoice_id] => 5
[Amount] => 10000
[Type] => Debit
[Method] => Sale
)
[3] => Array
(
[ID] => 48
[Create_date] => 2018-07-10
[Create_time] => 16:59:32
[Invoice_series] => VS
[Invoice_id] => 6
[Amount] => 16757
[Type] => Debit
[Method] => Sale
)
[4] => Array
(
[ID] => 9
[Create_date] => 2018-07-28
[Create_time] => 15:10:33
[Invoice_series] => SRT
[Invoice_id] => 1
[Amount] => 1237
[Type] => Credit
[Method] => sale_return
)
[5] => Array
(
[ID] => 10
[Create_date] => 2018-09-17
[Create_time] => 15:43:30
[Invoice_series] => SRT
[Invoice_id] => 2
[Amount] => 361
[Type] => Credit
[Method] => sale_return
)
[6] => Array
(
[ID] => 14
[Create_date] => 2018-09-18
[Create_time] => 10:06:52
[Invoice_series] => SRT
[Invoice_id] => 4
[Amount] => 6262
[Type] => Credit
[Method] => sale_return
)
[7] => Array
(
[ID] => 13
[Create_date] => 2018-09-18
[Create_time] => 10:04:38
[Invoice_series] => SRT
[Invoice_id] => 3
[Amount] => 4307
[Type] => Credit
[Method] => sale_return
)
[8] => Array
(
[ID] => 15
[Create_date] => 2018-09-29
[Create_time] => 17:02:39
[Invoice_series] => SRT
[Invoice_id] => 5
[Amount] => 2253
[Type] => Credit
[Method] => sale_return
)
[9] => Array
(
[ID] => 76
[Create_date] => 2018-10-01
[Create_time] => 11:44:20
[Invoice_series] => VS
[Invoice_id] => 14
[Amount] => 1799
[Type] => Debit
[Method] => Sale
)
[10] => Array
(
[ID] => 78
[Create_date] => 2018-10-01
[Create_time] => 18:44:08
[Invoice_series] => VS
[Invoice_id] => 16
[Amount] => 19656
[Type] => Debit
[Method] => Sale
)
[11] => Array
(
[ID] => 77
[Create_date] => 2018-10-01
[Create_time] => 18:41:00
[Invoice_series] => VS
[Invoice_id] => 15
[Amount] => 1213
[Type] => Debit
[Method] => Sale
)
[12] => Array
(
[ID] => 89
[Create_date] => 2018-10-04
[Create_time] => 10:02:50
[Invoice_series] => VS
[Invoice_id] => 17
[Amount] => 10251
[Type] => Debit
[Method] => Sale
)
[13] => Array
(
[ID] => 18
[Create_date] => 2018-10-12
[Create_time] => 11:14:34
[Invoice_series] => SRT
[Invoice_id] => 6
[Amount] => 3186
[Type] => Credit
[Method] => sale_return
)
[14] => Array
(
[ID] => 94
[Create_date] => 2018-10-22
[Create_time] => 14:58:22
[Invoice_series] => VS
[Invoice_id] => 21
[Amount] => 9251
[Type] => Debit
[Method] => Sale
)
[15] => Array
(
[ID] => 29
[Create_date] => 2018-10-24
[Create_time] => 19:44:03
[Invoice_series] => SRT
[Invoice_id] => 7
[Amount] => 5000
[Type] => Credit
[Method] => sale_return
)
[16] => Array
(
[ID] => 97
[Create_date] => 2018-10-24
[Create_time] => 11:47:51
[Invoice_series] => VS
[Invoice_id] => 22
[Amount] => 5000
[Type] => Debit
[Method] => Sale
)
[17] => Array
(
[ID] => 98
[Create_date] => 2018-10-24
[Create_time] => 15:02:05
[Invoice_series] => VS
[Invoice_id] => 23
[Amount] => 5000
[Type] => Debit
[Method] => Sale
)
[18] => Array
(
[ID] => 30
[Create_date] => 2018-10-25
[Create_time] => 16:52:29
[Invoice_series] => SRT
[Invoice_id] => 8
[Amount] => 10251
[Type] => Credit
[Method] => sale_return
)
[19] => Array
(
[ID] => 51
[Create_date] => 2018-10-26
[Create_time] => 15:25:36
[Invoice_series] => SRT
[Invoice_id] => 10
[Amount] => 5000
[Type] => Credit
[Method] => sale_return
)
[20] => Array
(
[ID] => 109
[Create_date] => 2018-10-26
[Create_time] => 18:26:13
[Invoice_series] => VS
[Invoice_id] => 24
[Amount] => 10251
[Type] => Debit
[Method] => Sale
)
[21] => Array
(
[ID] => 50
[Create_date] => 2018-10-26
[Create_time] => 12:00:51
[Invoice_series] => SRT
[Invoice_id] => 9
[Amount] => 5000
[Type] => Credit
[Method] => sale_return
)
[22] => Array
(
[ID] => 52
[Create_date] => 2018-10-27
[Create_time] => 18:14:15
[Invoice_series] => SRT
[Invoice_id] => 11
[Amount] => 10251
[Type] => Credit
[Method] => sale_return
)
[23] => Array
(
[ID] => 1020
[Create_date] => 2018-11-23
[Create_time] => 17:30:14
[Payment_series] => RecT
[Payment_id] => 3
[Amount] => 9251
[Type] => Credit
[Method] => payment
)
[24] => Array
(
[ID] => 1021
[Create_date] => 2018-11-23
[Create_time] => 17:30:33
[Payment_series] => RecT
[Payment_id] => 4
[Amount] => 0
[Type] => Debit
[Method] => payment
)
[25] => Array
(
[ID] => 1016
[Create_date] => 2018-11-23
[Create_time] => 11:28:57
[Payment_series] => RecT
[Payment_id] => 1
[Amount] => 20251
[Type] => Credit
[Method] => payment
)
[26] => Array
(
[ID] => 1018
[Create_date] => 2018-11-23
[Create_time] => 16:08:17
[Payment_series] => RecT
[Payment_id] => 2
[Amount] => -20251
[Type] => Debit
[Method] => payment
)
)
PHP代码:
按时间排序
usort($Ledgers, function( $a, $b ) {return strtotime($a["Create_time"]) - strtotime($b["Create_time"]);});
明智地排序日期
usort($Ledgers, function( $a, $b ) {return strtotime($a["Create_date"]) - strtotime($b["Create_date"]);});
答案 0 :(得分:0)
您只需先连接日期和时间
usort($Ledgers, function( $a, $b ) {
$create_a = strtotime($a["Create_date"].$a["Create_time"]);
$create_b = strtotime($b["Create_date"].$b["Create_time"]);
return $create_a - $create_b;
});
并使用usort函数一次。
另一个解决方案是使用strcmp
对字符串进行排序
usort($Ledgers, function ($a, $b) {
return strcmp($a['Create_date'].$a['Create_time'], $b['Create_date'].$b['Create_time']);
});