我有一個意想不到的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"
}
}
正如你可以看到陣列未排序喜歡我怎麼想的那樣。我想要最低的時間戳。誰能幫我嗎?
'return $ a ['datestamp']> $ b ['datestamp'];'? –
沒有工作...數組排序就像array_reverse() –