我用数据库中的数据填充了这个数组
$collectTable1 = array( 'errand' => $interest->errand_id,
'timestamp' => $interest->timestamp,
'type' => $interest->type,
'amount' => $interest->amount
);
$collector[] = $collectTable1;
我想像这样对时间戳进行排序
$sortTime = rsort($collectedData['timestamp']);
我尝试了这个,我得到了这个输出
function timesort($a, $b) { return (intval($a['timestamp']) > intval($b['timestamp'])); } usort($collector, "timesort");
2017-12-01 10:53:26
我想我从下降的日期点开始吗?就像是 2018-09-04 12:32:16
我的时间戳还包含unixtimestamp和常规日期,例如“ 2017-12-01 10:53:26“
答案 0 :(得分:2)
我猜你在$collector
中有元素数组。
如果您想按timestamp
对它们进行排序,则可以使用usort
考虑以下示例:
$collector = array();
$e1 = array("errand" => 1, "timestamp" => "2017-12-01 10:53:26");
$e2 = array("errand" => 2, "timestamp" => "2018-07-01 10:53:26");
$e3 = array("errand" => 3, "timestamp" => "2018-12-01 10:53:26");
$collector = array($e1, $e2, $e3);
function cmp($a, $b)
{
return (strtotime($a['timestamp']) < strtotime($b['timestamp']));
}
usort($collector, "cmp");
当您的timestamp
值是字符串时,请使用strtotime将它们转换为EPOC,然后再进行比较。
现在,$collector
数组元素按timestamp
值排序。
代码示例的输出为:
Array
(
[0] => Array
(
[errand] => 3
[timestamp] => 2018-12-01 10:53:26
)
[1] => Array
(
[errand] => 2
[timestamp] => 2018-07-01 10:53:26
)
[2] => Array
(
[errand] => 1
[timestamp] => 2017-12-01 10:53:26
)
)
答案 1 :(得分:0)
一旦有了阵列:
<?php
$data = [
['timestamp' => '100'],
['timestamp' => '300'],
['timestamp' => '200']
];
usort($data, function($a, $b) {
return $b['timestamp'] <=> $a['timestamp'];
});
var_export($data);
输出:
array (
0 =>
array (
'timestamp' => '300',
),
1 =>
array (
'timestamp' => '200',
),
2 =>
array (
'timestamp' => '100',
),
)
答案 2 :(得分:0)
如果关联数组$ collectTable1的所有键的值比
多foreach($interest as $i){
$collectTable1 = array( 'errand' => array($i->errand_id),
'timestamp' => array($i->timestamp),
'type' => array($i->type),
'amount' => array($i->amount)
);
rsort($collectTable1[‘timestamp’]);
在这里,collectTable1是一维数组的关联数组。
$ collectTable1 [‘timestamp’] [0] = firstvalue $ collectTable1 ['timestamp'] [1] = secondvalue
依此类推