需要有關排序要求的幫助 (Need assistance with a usort requirement)


問題描述

需要有關排序要求的幫助 (Need assistance with a usort requirement)

I've taken a look at a few googles and actually found this stack too: How to sort a date array in PHP

I'ts on the same wave length but i'm not sure i get it clearly. I've read over the phpnet documentation too... As I said, i'm feeling shakey from it.

I have an array:

Array
(
    [0] => Array
        (
            [created] => 2012-06-06 21:26:25
        )

    [1] => Array
        (
            [created] => 2012-06-06 21:23:45
        )

)

And I basically need to sort this array so that they are in date order. How does this call back function work for usort? Any examples would be great!


參考解法

方法 1:

function MySort($a, $b)
{
    if ($a['created'] == $b['created']) return 0;
    return $a['created'] < $b['created'] ? -1 : 1;
}

then use...

usort($myarray, "MySort");

方法 2:

usort just lets you sort using your own criteria.  You can just simply do this:

usort($array, function($a, $b){
    $a = strtotime($a['created']);
    $b = strtotime($b['created']);
    return $a-$b;
});

(by Jimmyt1988craig1231gen_Eric)

參考文件

  1. Need assistance with a usort requirement (CC BY-SA 3.0/4.0)

#Date #PHP #usort






相關問題

MySQL WHERE 時間戳 >= SUBDATE(MAX(timestamp), INTERVAL 5 DAY) (MySQL WHERE timestamp >= SUBDATE(MAX(timestamp), INTERVAL 5 DAY))

需要有關排序要求的幫助 (Need assistance with a usort requirement)

在 Java 日期/日曆對像中發現神秘的毫秒數 (Mysterious milliseconds number found in Java Date/Calendar object)

按日期子集 data.frame (Subset data.frame by date)

根據添加到頁面的內容更改 div 類 (change div class based on content added to page)

我應該如何設置日期? (How should I set the date?)

日期的 Drupal 8 上下文過濾器 (Drupal 8 contextual filter for date)

是什麼導致了這個無效的日期錯誤? (What is causing this invalid date error?)

按存儲在文本列中的日期排序表 (Order table by date stored in text column)

如何啟用靜態時間但仍有動態日期? (How do I enable a static time but still have dynamic date?)

是否可以在日期列中加載各種格式的數據?甲骨文 (is that possible to load data of various format in date column | Oracle)

MongoDB/Mongoose 時區日期遞減問題 (MongoDB/Mongoose timezone date decrement problem)







留言討論