簡體   English   中英

如何按日期對PHP進行排序

[英]How to sort a PHP by date

這是我的數組:

Array ( 
    [0] => Array ( [0] => content here [1] => 2010-02-04 01:25:34 )
    [1] => Array ( [0] => content here [1] => 2010-02-04 04:51:37 ) 
    [2] => Array ( [0] => content here [1] => 2010-02-04 04:52:31 ) 
    [3] => Array ( [0] => content here [1] => 2010-02-04 05:50:48 ) 
    [4] => Array ( [0] => content here [1] => 2010-02-04 03:25:34 ) 
    [5] => Array ( [0] => content here [1] => 2010-02-04 05:39:33 ) 
    [6] => Array ( [0] => content here [1] => 2010-02-04 03:25:34 ) 
    [7] => Array ( [0] => content here [1] => 2010-02-04 07:07:09 ) 
    [8] => Array ( [0] => content here [1] => 2010-02-04 07:07:23 ) 
    [9] => Array ( [0] => content here [1] => 2010-02-04 08:51:18 ) 
) 

如何按時間戳排序?

或與strtotime()一起使用usort() strtotime()

function compare($e1, $e2) {
    $t1 = strtotime($e1[1]));
    $t2 = strtotime($e2[1]));

    if($t1 == t2) {
       return 0;
    }
    return ($t1 > $t2) ? 1 : -1;
}


usort($array, 'compare');

usort()usort()一起使用,該cmp_function比較每個傳遞參數的索引1。

使用array_multisort

array_multisort()這是一個令人討厭但功能強大的小功能。 基本上,您必須遍歷數組,將日期戳拉到新數組中-維護鍵關聯。 然后,對該新數組進行排序,仍然保持鍵關聯。 然后將新排序的數組和原始數組都放入array_multisort()中,原始數組將被排序為具有與排序數組相同順序的鍵。

像泥一樣清澈? 該文檔頁面上的示例應有所幫助。

冒泡排序怎么

這意味着循環瀏覽每個日期,檢查前一個日期是否更大,等等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM