2017-01-27 63 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来测试这个,我会考虑升级... –