【发布时间】:2017-12-26 12:53:59
【问题描述】:
我有一个这样的 php 数组:-
array(3) {
[0]=>
string(4) "9:30"
[1]=>
string(5) "15:00"
[2]=>
string(5) "13:00"
}
这次排序后,我得到了错误的数据。我正在按array_multisort 函数对其进行排序。
这是我在时间排序后得到的
array(3) {
[0]=>
string(5) "03:00"
[1]=>
string(5) "01:00"
[2]=>
string(5) "09:30"
}
这是我的代码
首先我通过使用 strtotime 函数将我的时间转换为 unix 时间,然后像这样排序:
while($row11 = mysqli_fetch_array($hourget))
{
$totlahourdat1[] = strtotime($row11['hours']);
$totlahourdat2[] = $row11['hours'];
}
echo "<pre>";
var_dump($totlahourdat2);
echo "</pre>";
//sort data
array_multisort($totlahourdat1, SORT_DESC);
//var_dump($totlahourdat);
foreach($totlahourdat1 as $time){
$totlahourdat[] = date("h:i",$time);
}
echo "<pre>";
var_dump($totlahourdat);
echo "</pre>";
如果我打印我的 $totlahourdat1 数组,那么我会得到这个:
array(3) {
[0]=>
int(1500535800)
[1]=>
int(1500555600)
[2]=>
int(1500548400)
}
我的结果应该是这样的:
array(3) {
[0]=>
string(4) "9:30"
[1]=>
string(5) "13:00"
[2]=>
string(5) "15:00"
}
我们将不胜感激。
【问题讨论】:
标签: php arrays sorting datetime time