2017-01-27 114 views
1

我有一個意想不到的usort結果。php排序時間戳陣列

$ccpmail[] = 
    array(
      "mail_id"=>$evemailheader["mail_id"], 
      "is_read"=>$evemailheader['is_read'],           
      "fromid"=>$evemailheader['from'], 
      "fromname"=>$character_names[get_character_name($evemailheader['from'], $character_names)]['character_name'], 
      "subject"=>$evemailheader['subject'], 
      "labels"=>array("EVE Online"), 
      'datestamp'=>strtotime($evemailheader['timestamp']), 
      'date'=>$date 
     ); 

$email[] = 
    array(
     "mail_id"=>$mailid, 
     "is_read"=>$is_read,            
     "fromid"=>$message_parsed->getHeaderValue('from'), 
     "fromname"=>$message_parsed->getHeader('from')->getPersonName(), 
     "subject"=>$message_parsed->getHeaderValue('subject'), 
     "labels"=>array("E-mail"), 
     'datestamp'=>strtotime($message_parsed->getHeaderValue('date')), 
     'date'=>$date->format('Y-m-d H:i:s') 
    ); 


$allmail = array_merge($ccpmail, $email); 

function sortByOrder($a, $b) { 
return $a['datestamp'] < $b['datestamp']; 
} 

usort($allmail, 'sortByOrder'); 
var_dump($allmail); 

的結果:

array(133) { 
[0]=> 
array(8) { 
    ["mail_id"]=> 
    string(32) "bcfc5f6b0365738e58c99e0501b37210" 
    ["is_read"]=> 
    bool(true) 
    ["fromid"]=> 
    string(22) "EMAIL" 
    ["fromname"]=> 
    string(12) "NAME" 
    ["subject"]=> 
    string(4) "tset" 
    ["labels"]=> 
    array(1) { 
     [0]=> 
     string(6) "E-mail" 
    } 
    ["datestamp"]=> 
    int(1485509147) 
    ["date"]=> 
    string(19) "2017-01-27 09:25:47" 
} 
[1]=> 
array(8) { 
    ["mail_id"]=> 
    string(32) "317c556f35213548598f0bb838a237c1" 
    ["is_read"]=> 
    bool(true) 
    ["fromid"]=> 
    string(22) "EMAIL" 
    ["fromname"]=> 
    string(12) "NAME" 
    ["subject"]=> 
    string(5) "etest" 
    ["labels"]=> 
    array(1) { 
     [0]=> 
     string(6) "E-mail" 
    } 
    ["datestamp"]=> 
    int(1485509066) 
    ["date"]=> 
    string(19) "2017-01-27 09:24:26" 
} 
[2]=> 
array(8) { 
    ["mail_id"]=> 
    int(364172046) 
    ["is_read"]=> 
    bool(false) 
    ["fromid"]=> 
    int(90766569) 
    ["fromname"]=> 
    string(13) "NAME" 
    ["subject"]=> 
    string(2) "hi" 
    ["labels"]=> 
    array(1) { 
     [0]=> 
     string(10) "EVE Online" 
    } 
    ["datestamp"]=> 
    int(1485507000) 
    ["date"]=> 
    object(DateTime)#3 (3) { 
     ["date"]=> 
     string(26) "2017-01-27 08:50:00.000000" 
     ["timezone_type"]=> 
     int(2) 
     ["timezone"]=> 
     string(1) "Z" 
    } 
} 
[3]=> 
array(8) { 
    ["mail_id"]=> 
    int(364160956) 
    ["is_read"]=> 
    bool(true) 
    ["fromid"]=> 
    int(793110520) 
    ["fromname"]=> 
    string(10) "NAME" 
    ["subject"]=> 
    string(30) "theres no brakes on this train" 
    ["labels"]=> 
    array(1) { 
     [0]=> 
     string(10) "EVE Online" 
    } 
    ["datestamp"]=> 
    int(1485455100) 
    ["date"]=> 
    object(DateTime)#7 (3) { 
     ["date"]=> 
     string(26) "2017-01-26 18:25:00.000000" 
     ["timezone_type"]=> 
     int(2) 
     ["timezone"]=> 
     string(1) "Z" 
    } 
} 
[4]=> 
array(8) { 
    ["mail_id"]=> 
    int(364160959) 
    ["is_read"]=> 
    bool(true) 
    ["fromid"]=> 
    int(793110520) 
    ["fromname"]=> 
    string(10) "NAME" 
    ["subject"]=> 
    string(10) "safe word?" 
    ["labels"]=> 
    array(1) { 
     [0]=> 
     string(10) "EVE Online" 
    } 
    ["datestamp"]=> 
    int(1485455100) 
    ["date"]=> 
    object(DateTime)#5 (3) { 
     ["date"]=> 
     string(26) "2017-01-26 18:25:00.000000" 
     ["timezone_type"]=> 
     int(2) 
     ["timezone"]=> 
     string(1) "Z" 
    } 
} 
[5]=> 
array(8) { 
    ["mail_id"]=> 
    int(364160957) 
    ["is_read"]=> 
    bool(true) 
    ["fromid"]=> 
    int(1228369447) 
    ["fromname"]=> 
    string(7) "NAME" 
    ["subject"]=> 
    string(4) "spam" 
    ["labels"]=> 
    array(1) { 
     [0]=> 
     string(10) "EVE Online" 
    } 
    ["datestamp"]=> 
    int(1485455100) 
    ["date"]=> 
    object(DateTime)#6 (3) { 
     ["date"]=> 
     string(26) "2017-01-26 18:25:00.000000" 
     ["timezone_type"]=> 
     int(2) 
     ["timezone"]=> 
     string(1) "Z" 
    } 
} 
[6]=> 
array(8) { 
    ["mail_id"]=> 
    int(364160934) 
    ["is_read"]=> 
    bool(true) 
    ["fromid"]=> 
    int(94312752) 
    ["fromname"]=> 
    string(19) "NAME" 
    ["subject"]=> 
    string(4) "spam" 
    ["labels"]=> 
    array(1) { 
     [0]=> 
     string(10) "EVE Online" 
    } 
    ["datestamp"]=> 
    int(1485455040) 
    ["date"]=> 
    object(DateTime)#11 (3) { 
     ["date"]=> 
     string(26) "2017-01-26 18:24:00.000000" 
     ["timezone_type"]=> 
     int(2) 
     ["timezone"]=> 
     string(1) "Z" 
    } 
} 

正如你可以看到陣列未排序喜歡我怎麼想的那樣。我想要最低的時間戳。誰能幫我嗎?

+1

'return $ a ['datestamp']> $ b ['datestamp'];'? –

+0

沒有工作...數組排序就像array_reverse() –

回答

0

您的比較函數需要返回大於,小於或等於零的整數才能正確排序。 (See the definition of value_compare_func here.)這個表達式:

return $a['datestamp'] < $b['datestamp']; 

將只返回truefalse(1或0,),其既不是正確的值,以前後來的(-1)的較早時間戳排序。

您可以添加一個比較,以便它將正確排序所需的值。

function sortByOrder($a, $b) { 
    if ($a['datestamp'] < $b['datestamp']) return -1; // return -1 if $a is earlier 
    return $a['datestamp'] > $b['datestamp']; // return 1 if $a is later, or 0 if equal 
} 

正如其他答案指出,如果你使用PHP 7有操作(<=>),可以讓你用一個語句做到這一點。

+0

這有完全相同的結果不幸 –

+0

@ S.R。它適用於我:https://3v4l.org/AsbCB –

+0

我加倍檢查的東西,並確認工作 –

1

如果使用PHP7或更高版本,可以使用的飛船運營商,像這樣:

function sortByOrder($a, $b) { 
    return $a['datestamp'] <=> $b['datestamp']; 
} 

像你想這將對其進行排序。如果不使用PHP7或更高版本,則必須製作一些if語句以查看它是否更高,更低或相等。

+0

我沒有運行PHP7來測試這個,我會考慮升級... –