按日期时间键对关联数组排序
Sort Associate array by datetime key
我有一个像这样具有不同索引位置的数组。
Array
(
[0] => Array
(
[datetime] => 27/01/2017 12:18
[location] => Raunheim (DE)
[date] => 27/01/2017
[time] => 12:18
[status] => Erfolgreich zugestellt.
[status_1] => (Retoure an Versender)
)
[2] => Array
(
[datetime] => 11/01/2017 16:10
[location] => Vlotho (DE)
[date] => 11/01/2017
[time] => 16:10
[status] => Ihr Paket konnte nicht wie geplant zugestellt werden und ist wieder im Paketzustellzentrum.
)
[2] => Array
(
[datetime] => 25/01/2017 11:24
[status] => Erfolgreich zugestellt.
[status_id] =>
[date] => 25/01/2017
[location] => Altentreptow (DE)
[time] => 11:24
)
)
我想使用日期时间键对该数组进行排序。我已经尝试过解决方案
usort($second_tracking_array, 'date_compare');
function date_compare($a, $b) {
$date1 = $a['datetime'];
$date2 = $b['datetime'];
$t1 = strtotime($date1);
$t2 = strtotime($date2);
echo $t1 . " : " . $t2 . "</br>";
return $t2 - $t1;
}
但是数组没有排序。在调试时,我发现这些函数只有在所有索引位置都正确时才有效并对数组进行排序。但就我而言,我的一些数组索引位置不同。
您需要更改解析日期字符串的策略。原因是您的日期时间字符串 显然 的格式不是任何符合标准的变体。
<?php
define('CUSTOM_DATE_FORMAT', 'd/m/Y G:i');
$second_tracking_array = [
[
'datetime' => '27/01/2017 12:18',
'location' => 'Raunheim (DE)',
'date' => '27/01/2017',
'time' => '12:18',
'status' => 'Erfolgreich zugestellt.',
'status_1' => '(Retoure an Versender)'
],
[
'datetime' => '11/01/2017 16:10',
'location' => 'Vlotho (DE)',
'date' => '11/01/2017',
'time' => '16:10',
'status' => 'Ihr Paket konnte nicht wie geplant zugestellt werden und ist wieder im Paketzustellzentrum.'
],
[
'datetime' => '25/01/2017 11:24',
'status' => 'Erfolgreich zugestellt.',
'status_id' => '',
'date' => '25/01/2017',
'location' => 'Altentreptow (DE)',
'time' => '11:24'
]
];
usort(
$second_tracking_array,
function($a, $b) {
$date1 = DateTime::createFromFormat(CUSTOM_DATE_FORMAT, $a['datetime']);
$date2 = DateTime::createFromFormat(CUSTOM_DATE_FORMAT, $b['datetime']);
return $date1 > $date2;
}
);
print_r($second_tracking_array);
上面的明显输出是:
Array
(
[0] => Array
(
[datetime] => 11/01/2017 16:10
[location] => Vlotho (DE)
[date] => 11/01/2017
[time] => 16:10
[status] => Ihr Paket konnte nicht wie geplant zugestellt werden und ist wieder im Paketzustellzentrum.
)
[1] => Array
(
[datetime] => 25/01/2017 11:24
[status] => Erfolgreich zugestellt.
[status_id] =>
[date] => 25/01/2017
[location] => Altentreptow (DE)
[time] => 11:24
)
[2] => Array
(
[datetime] => 27/01/2017 12:18
[location] => Raunheim (DE)
[date] => 27/01/2017
[time] => 12:18
[status] => Erfolgreich zugestellt.
[status_1] => (Retoure an Versender)
)
)
我有一个像这样具有不同索引位置的数组。
Array
(
[0] => Array
(
[datetime] => 27/01/2017 12:18
[location] => Raunheim (DE)
[date] => 27/01/2017
[time] => 12:18
[status] => Erfolgreich zugestellt.
[status_1] => (Retoure an Versender)
)
[2] => Array
(
[datetime] => 11/01/2017 16:10
[location] => Vlotho (DE)
[date] => 11/01/2017
[time] => 16:10
[status] => Ihr Paket konnte nicht wie geplant zugestellt werden und ist wieder im Paketzustellzentrum.
)
[2] => Array
(
[datetime] => 25/01/2017 11:24
[status] => Erfolgreich zugestellt.
[status_id] =>
[date] => 25/01/2017
[location] => Altentreptow (DE)
[time] => 11:24
)
)
我想使用日期时间键对该数组进行排序。我已经尝试过解决方案
usort($second_tracking_array, 'date_compare');
function date_compare($a, $b) {
$date1 = $a['datetime'];
$date2 = $b['datetime'];
$t1 = strtotime($date1);
$t2 = strtotime($date2);
echo $t1 . " : " . $t2 . "</br>";
return $t2 - $t1;
}
但是数组没有排序。在调试时,我发现这些函数只有在所有索引位置都正确时才有效并对数组进行排序。但就我而言,我的一些数组索引位置不同。
您需要更改解析日期字符串的策略。原因是您的日期时间字符串 显然 的格式不是任何符合标准的变体。
<?php
define('CUSTOM_DATE_FORMAT', 'd/m/Y G:i');
$second_tracking_array = [
[
'datetime' => '27/01/2017 12:18',
'location' => 'Raunheim (DE)',
'date' => '27/01/2017',
'time' => '12:18',
'status' => 'Erfolgreich zugestellt.',
'status_1' => '(Retoure an Versender)'
],
[
'datetime' => '11/01/2017 16:10',
'location' => 'Vlotho (DE)',
'date' => '11/01/2017',
'time' => '16:10',
'status' => 'Ihr Paket konnte nicht wie geplant zugestellt werden und ist wieder im Paketzustellzentrum.'
],
[
'datetime' => '25/01/2017 11:24',
'status' => 'Erfolgreich zugestellt.',
'status_id' => '',
'date' => '25/01/2017',
'location' => 'Altentreptow (DE)',
'time' => '11:24'
]
];
usort(
$second_tracking_array,
function($a, $b) {
$date1 = DateTime::createFromFormat(CUSTOM_DATE_FORMAT, $a['datetime']);
$date2 = DateTime::createFromFormat(CUSTOM_DATE_FORMAT, $b['datetime']);
return $date1 > $date2;
}
);
print_r($second_tracking_array);
上面的明显输出是:
Array
(
[0] => Array
(
[datetime] => 11/01/2017 16:10
[location] => Vlotho (DE)
[date] => 11/01/2017
[time] => 16:10
[status] => Ihr Paket konnte nicht wie geplant zugestellt werden und ist wieder im Paketzustellzentrum.
)
[1] => Array
(
[datetime] => 25/01/2017 11:24
[status] => Erfolgreich zugestellt.
[status_id] =>
[date] => 25/01/2017
[location] => Altentreptow (DE)
[time] => 11:24
)
[2] => Array
(
[datetime] => 27/01/2017 12:18
[location] => Raunheim (DE)
[date] => 27/01/2017
[time] => 12:18
[status] => Erfolgreich zugestellt.
[status_1] => (Retoure an Versender)
)
)