2017-01-27 77 views
1

我有一個像這樣的具有不同索引位置的數組。按日期時間鍵排序關聯數組

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 
    ) 
) 

我想用datetime鍵對這個數組進行排序。我已經嘗試過解決方案

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; 
} 

但是數組沒有排序。在調試時,我發現只有所有索引位置都正確時,函數才能工作並對數組進行排序。但在我的情況下,我的一些數組索引位置是不同的。

+0

你不能有相同的鍵2次,就像在你的榜樣(2X鍵2)。你可以用'$ array = array_values($ array);' – pixelarbeit

+0

修復你的數組鍵值。但是每個數組索引只包含一個日期時間鍵。我正面臨的問題在函數date_compare我經常得到$ t2空。只有在兩個比較數組都具有相同結構數組的情況下,我纔會得到帶有時間戳的$ t1和$ t2 –

回答

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) 
     ) 

) 
+0

感謝此工作。 –

+0

@HabibQadoos當然,歡迎您。我只是通過去除對'getTimestamp()'的調用來進一步簡化代碼,這實際上並不是必需的。 – arkascha