[英]How to sort an array that contains collection in laravel?
[
{
"clubName": "FC Nightmare",
"win": 0,
"played": 1,
"draw": 0,
"lose": 1,
"gs": 1,
"ga": 2,
"gd": -1,
"points": 0
},
{
"clubName": "Manchester City",
"win": 1,
"played": 1,
"draw": 0,
"lose": 0,
"gs": 3,
"ga": 2,
"gd": 1,
"points": 3
},
{
"clubName": "Inter Milan",
"win": 0,
"played": 1,
"draw": 0,
"lose": 1,
"gs": 2,
"ga": 3,
"gd": -1,
"points": 0
},
{
"clubName": "AC Milan",
"win": 1,
"played": 1,
"draw": 0,
"lose": 0,
"gs": 2,
"ga": 1,
"gd": 1,
"points": 3
}
]
i have this array.i want to sort this array by points.how to do that in laravel?? 我有这个array.i我想按点对这个数组进行排序。如何在laravel中做到这一点?
i've tried this: 我已经试过了:
array_multisort(array_column($point_table,'points'),$point_table);
在array_multisort(array_column($ point_table, '分'),$ point_table);
but it doesn't work.. 但这不起作用..
If you are using Laravel, which your tag suggests, you can use collections to manipulate arrays like this. 如果使用标签建议的Laravel,则可以使用集合来操纵数组。 For example:
例如:
$array = collect($array)->sortBy('count')->reverse()->toArray();
Or 要么
Using array_multisort() 使用array_multisort()
$array = array(
46 =>
array (
'name' => 'HSR Layout',
'url' => 'hsr-layout',
'count' => 2,
),
37 =>
array (
'name' => 'Electronic City',
'url' => 'electronic-city',
'count' => 3,
)
);
$price = array();
foreach ($array as $key => $row)
{
$count[$key] = $row['count'];
}
array_multisort($count, SORT_DESC, $array);
print_r($array);
You can do it with simple php-function usort
. 您可以使用简单的php函数
usort
做到这一点。
But also with laravel's collection: 还要配合laravel的收藏:
collect($point_table)->sortBy('points');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.