这是一个数组还是不是?

Is this a array or not?

我用数据库中的数据填充了这个数组

$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-0110:53:26

我想我会从下降的日期点开始?就像是 2018-09-04 12:32:16。

我的时间戳还包含 unixtimestamp 和常规日期,如下所示“ 2017-12-01 10:53:26

我猜你有 $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
        )
)

获得数组后:

<?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',
    ),
)

如果关联数组 $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

以此类推