-2

首先我为我的英语道歉。我有一个来自 json 格式的确实 api 的数组,我正在尝试按日期值对数组进行排序,这确实是 api 的结果

            [0] => stdClass Object
            (
                [jobtitle] => MUDLOGGER
                [company] => Weatherford
                [city] => South Jakarta
                [state] => JK
                [country] => ID
                [formattedLocation] => South Jakarta
                [source] => Weatherford
                [date] => Tue, 03 Sep 2013 14:15:56 GMT
                [onmousedown] => indeed_clk(this, '7414');
                [latitude] => -6.266483
                [longitude] => 106.8022
                [jobkey] => a7490605224a8d72
                [sponsored] => 
                [expired] => 
                [formattedLocationFull] => South Jakarta
                [formattedRelativeTime] => 12 hari yang lalu
            )

        [1] => stdClass Object
            (
                [jobtitle] => Quality Assurance & Control
                [company] => PT Indo Meco Primatama
                [city] => Depok
                [state] => JB
                [country] => ID
                [formattedLocation] => Depok
                [source] => CareerBuilder
                [date] => Fri, 13 Sep 2013 10:18:10 GMT
                [[onmousedown] => indeed_clk(this, '7414');
                [latitude] => -6.384615
                [longitude] => 106.82967
                [jobkey] => f1808c40b46eebaa
                [sponsored] => 
                [expired] => 
                [formattedLocationFull] => Depok
                [formattedRelativeTime] => 2 hari yang lalu
            )

还有我的代码

for ($x = 0; $x <= 9; $x++) {
echo $data[$x]->date;
}

提前致谢

4

2 回答 2

2

您可以将其传递给您自己的排序函数

function sort_by_date($a, $b) {
   $a = strtotime($a->date);
   $b = strtotime($b->date);
   return ($a < $b) ? -1 : 1;
}

uasort($array, 'sort_by_date');
于 2013-09-15T19:41:55.053 回答
1

使用usort()功能:

usort($data, function(stdClass $a, stdClass $b) {
    $aDate = strtotime($a->date);
    $bDate = strtotime($b->date);

    if($aDate == $bDate) return 0;
    return $aDate > $bDate ? 1 : -1;
});
于 2013-09-15T19:45:35.803 回答