对 php 中的数组对象进行排序
Sorting array object in php
Array
(
[0] => Array
(
[post_id] => 70
[percentage] => 66.666666666667
)
[1] => Array
(
[post_id] => 72
[percentage] => 44.444444444444
)
[2] => Array
(
[post_id] => 74
[percentage] => 11.111111111111
)
[3] => Array
(
[post_id] => 82
[percentage] => 60
)
)
如何根据百分比按降序对数组进行排序我尝试使用此代码,但它不起作用
usort($post_result, array($this, "myfunction"));
function myfunction($a, $b)
{
return strcmp($a->percentage, $b->percentage);
}
试试这个:
usort($post_result, function($a, $b) {
if($a['percentage']==$b['percentage']) return 0;
return $a['percentage'] < $b['percentage']?1:-1;
});
Array
(
[0] => Array
(
[post_id] => 70
[percentage] => 66.666666666667
)
[1] => Array
(
[post_id] => 72
[percentage] => 44.444444444444
)
[2] => Array
(
[post_id] => 74
[percentage] => 11.111111111111
)
[3] => Array
(
[post_id] => 82
[percentage] => 60
)
)
如何根据百分比按降序对数组进行排序我尝试使用此代码,但它不起作用
usort($post_result, array($this, "myfunction"));
function myfunction($a, $b)
{
return strcmp($a->percentage, $b->percentage);
}
试试这个:
usort($post_result, function($a, $b) {
if($a['percentage']==$b['percentage']) return 0;
return $a['percentage'] < $b['percentage']?1:-1;
});