将数组重构为按日期降序排列



我有一个多维数组,它的索引['dates'] .['dates']里面有另一个索引,里面有一个带有一些数据的['date_time']索引。

目前,数组这部分的顺序是从最旧到最新的日期/时间。我希望扭转这种情况,以便第一个要素是最新的数据。

以下是数据的子集:

array(4) {
  [0]=>array(3) {
    ["id"]=>string(4) "1279"
    ["name"]=>string(13) "account_name"
    ["dates"]=>array(7) {
      [0]=>array(3) {
        ["date_time"]=>string(19) "2014-11-14 09:30:03"
        ["follower_count"]=>string(4) "1567"
        ["gains_losses"]=>string(4) "1567"
      }
      [1]=>array(3) {
        ["date_time"]=>string(19) "2014-11-14 16:30:35"
        ["follower_count"]=>string(4) "1566"
        ["gains_losses"]=>string(2) "-1"
      }...

这是完整的阵列

您可以将日期 obj 与格式一起使用,而不是 strtotime 以避免意外。

// Comparison function
function cmp($compare, $with) {
    $a = $compare['date_time'];
    $b = $with['date_time'];
    if (strtotime($a) == strtotime($b)) {
        return 0;
    }
    return ($a > $b) ? -1 : 1;
}
foreach($your_arr as &$item){
    uasort($item['dates'], 'cmp');
}

最新更新