经常在代码中我们需要实现数组排序,或者数组过滤,或者数组查找类似查找数据库一样的用法
可以使用collect
$items=[
["num"=>17,"status"=>0,"grade"=>5],
["num"=>17,"status"=>2,"grade"=>4],
["num"=>7,"status"=>1,"grade"=>6],
["num"=>17,"status"=>0,"grade"=>3],
];
$ucsCollect=collect($items);
//过滤
$ucsCollect=$ucsCollect->filter(function ($item, $key) {
return $item['num'] > 14;
});
//倒叙排序
$ucsList=$ucsCollect->sortByDesc(function ($item, $key) {
return $item['grade'];
//正序排序
})->sortBy(function ($item, $key) {
return $item['status'];
})->toArray();
var_dump($ucsList);