【问题标题】:PHP Array sorting by DatePHP数组按日期排序
【发布时间】:2017-12-13 21:10:06
【问题描述】:
我有这张桌子:
Date | Tobic | Speaker
25.01.2013 | My topic one | my speaker one
28.05.2017 | My topic two | my speaker two
03.02.2014 | My topic three | my speaker three
如何将它加载到数组中,按日期排序然后打印出来?
【问题讨论】:
标签:
php
arrays
sorting
date
【解决方案1】:
下面应该按日期分组输出表格,然后按顺序进行排序
$stmt = $db->prepare('SELECT date,topic,speaker FROM table');
$stmt->execute();
var_dump($stmt->fetchAll(PDO::FETCH_GROUP | PDO::FETCH_UNIQUE));
只是一个概念;没有测试,但有一个想法
【解决方案2】:
使用函数strtotime()将字符串转换为时间戳,使用ksort()按键对数组进行排序。
$myPodcasts;
//loop at all rows of a table
foreach ($allFiles as $file) {
$myPodcasts[] = array(
'date' => strtotime('25.01.2013'),
'topic' => 'My topic one',
'speaker' => 'My Speaker one'
);
}
//Sort array by keys
ksort($myPodcasts);
排序可能性
•sort() - sort arrays in ascending order
•rsort() - sort arrays in descending order
•asort() - sort associative arrays in ascending order, according to the value
•ksort() - sort associative arrays in ascending order, according to the key
•arsort() - sort associative arrays in descending order, according to the value
•krsort() - sort associative arrays in descending order, according to the key