【问题标题】:Sort Associate array by datetime key按日期时间键对关联数组进行排序
【发布时间】:2017-06-13 04:35:56
【问题描述】:

我有一个像这样具有不同索引位置的数组。

Array
(
[0] => Array
    (
        [datetime] => 27/01/2017 12:18
        [location] => Raunheim (DE)
        [date] => 27/01/2017
        [time] => 12:18
        [status] => Erfolgreich zugestellt.
        [status_1] => (Retoure an Versender)
    )
[2] => Array
    (
        [datetime] => 11/01/2017 16:10
        [location] => Vlotho (DE)
        [date] => 11/01/2017
        [time] => 16:10
        [status] => Ihr Paket konnte nicht wie geplant zugestellt werden und ist wieder im Paketzustellzentrum.
    )
[2] => Array
    (
        [datetime] => 25/01/2017 11:24
        [status] => Erfolgreich zugestellt.
        [status_id] => 
        [date] => 25/01/2017
        [location] => Altentreptow (DE)
        [time] => 11:24
    )
)

我想使用日期时间键对该数组进行排序。我已经尝试过解决方案

usort($second_tracking_array, 'date_compare');
function date_compare($a, $b) {

    $date1 = $a['datetime'];
    $date2 = $b['datetime'];

    $t1 = strtotime($date1);
    $t2 = strtotime($date2);

    echo $t1 . " : " . $t2 . "</br>";
    return $t2 - $t1;
}

但是数组没有排序。在调试时,我发现只有当所有索引位置都正确时,这些函数才能工作并对数组进行排序。但在我的情况下,我的一些数组索引位置是不同的。

【问题讨论】:

  • 您不能像示例中那样拥有相同的键 2 次(2x 键 2)。您可以使用 $array = array_values($array); 修复您的数组键
  • 但是数组的每个索引只包含一个日期时间键。我面临的问题是,在函数 date_compare 中,我经常得到 $t2 为空。只有当两个比较数组都具有相同的结构化数组时,我才会得到带有时间戳的 $t1 和 $t2

标签: php arrays sorting datetime associative-array


【解决方案1】:

您需要更改策略来解析日期字符串。原因是您的日期时间字符串 显然 具有的格式不是任何符合标准的变体。

<?php

define('CUSTOM_DATE_FORMAT', 'd/m/Y G:i');

$second_tracking_array = [
    [
        'datetime' => '27/01/2017 12:18',
        'location' => 'Raunheim (DE)',
        'date' => '27/01/2017',
        'time' => '12:18',
        'status' => 'Erfolgreich zugestellt.',
        'status_1' => '(Retoure an Versender)'
    ],
    [
        'datetime' => '11/01/2017 16:10',
        'location' => 'Vlotho (DE)',
        'date' => '11/01/2017',
        'time' => '16:10',
        'status' => 'Ihr Paket konnte nicht wie geplant zugestellt werden und ist wieder im Paketzustellzentrum.'
    ],
    [
        'datetime' => '25/01/2017 11:24',
        'status' => 'Erfolgreich zugestellt.',
        'status_id' => '',
        'date' => '25/01/2017',
        'location' => 'Altentreptow (DE)',
        'time' => '11:24'
    ]
];

usort(
    $second_tracking_array,
    function($a, $b) {
        $date1 = DateTime::createFromFormat(CUSTOM_DATE_FORMAT, $a['datetime']);
        $date2 = DateTime::createFromFormat(CUSTOM_DATE_FORMAT, $b['datetime']);
        return $date1 > $date2;
    }
);

print_r($second_tracking_array);

上面的明显输出是:

Array
(
    [0] => Array
        (
            [datetime] => 11/01/2017 16:10
            [location] => Vlotho (DE)
            [date] => 11/01/2017
            [time] => 16:10
            [status] => Ihr Paket konnte nicht wie geplant zugestellt werden und ist wieder im Paketzustellzentrum.
        )

    [1] => Array
        (
            [datetime] => 25/01/2017 11:24
            [status] => Erfolgreich zugestellt.
            [status_id] =>
            [date] => 25/01/2017
            [location] => Altentreptow (DE)
            [time] => 11:24
        )

    [2] => Array
        (
            [datetime] => 27/01/2017 12:18
            [location] => Raunheim (DE)
            [date] => 27/01/2017
            [time] => 12:18
            [status] => Erfolgreich zugestellt.
            [status_1] => (Retoure an Versender)
        )

)

【讨论】:

  • @HabibQadoos 当然,不客气。我只是通过删除对getTimestamp() 的调用来进一步简化代码,这实际上并不是必需的。
猜你喜欢
  • 1970-01-01
  • 2013-04-23
  • 1970-01-01
  • 2021-09-19
  • 1970-01-01
  • 1970-01-01
  • 2020-01-20
  • 2021-01-11
  • 1970-01-01
相关资源
最近更新 更多