我有这个数组。我想排序这个,我该怎么做?
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”排序的所有帖子。我怎样才能做到这一点?
尝试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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句