php排序时间戳数组

时间:2017-01-27 21:35:29

标签: php timestamp usort

我有一个意想不到的结果。

$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"
    }
}

正如您所看到的,数组没有像我想要的那样排序。我想要最低的时间戳。任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

如果您使用PHP7或更高版本,您可以使用太空船运营商,如下所示:

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

这将按您的意愿排序。如果不使用PHP7或更高版本,则必须制作一些if语句,以查看它是否更高,更低或更低。

答案 1 :(得分: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,则会有一个运算符(<=>),它允许您使用一个语句执行此操作。