2017-07-08 36 views
-1

我有这个数组。我想分类这个,我怎么能做到这一点?如何按特定属性对PHP中的关联数组集合进行排序

array:2 [▼ 
     0 => Collection {#198 ▼ 
     #items: array:2 [▼ 

     0 => {#201 ▼ 

     +"id": 2; 
     +"title": "Now eldest"; 
     +"description": "Now eldest new tastes plenty mother called misery get." 
     +"created_at": "2017-07-07 16:34:00" 
     } 

     1 => {#197 ▼ 

     +"id": 6 
     +"title": "dffd" 
     +"description": "fdfdfdfdfdffdfdf" 
     +"created_at": "2017-07-07 10:23:00" 
     }] } 
     1 => Collection {#189 ▼ 
     #items: array:2 [▼ 

     0 => {#208 ▼ 

     +"id": 5 
     +"title": "However, if you are not using Homestead" 
     +"description": "The Laravel framework has a few system requirements." 
     +"created_at": "2017-07-07 13:37:00" 
     } 

     1 => {#196 ▼ 

     +"id": 7 
     +"title": "fedffdf" 
     +"description": "fdfdfdf" 
     +"created_at": "2017-07-07 09:12:00" 
     }] }] 

我工作的应用程序,我想在时间轴上显示按时间'created_at'排序的所有帖子。 我该怎么做?

+0

似乎是一个Laravel集合,如果是的话https://laravel.com/docs/5.4/collections#method-sortby –

+1

总是,如果获取数据从数据库中,在该级别进行排序并为您节省大量性能。 – Doug

回答

1

尝试usort

usort($collection, function(array $a, array $b) { 
    return $a["created_at"] <=> $b["created_at"]; 
}); 

如果您使用的是PHP的版本不具备<=>操作,试试这个来代替:

usort($collection, function(array $a, array $b) { 
    return $a["created_at"] >= $b["created_at"] ? 1 : -1; 
}); 

链接到文件:PHP: usort

相关问题