我的列表数组
Array
(
[0] => Array
(
[id] => 1000044375809533
[likes] => 10
[comments] => 6
[posts] => 41
[gender] => female
)
[1] => Array
(
[id] => 1000043758049533
[likes] => 10
[comments] => 6
[posts] => 41
[gender] => male
)
[2] => Array
(
[id] => 1000040382928633
[likes] => 4
[comments] => 0
[posts] => 0
[gender] => female
)
[3] => Array
(
[id] => 1000003829428633
[likes] => 4
[comments] => 0
[posts] => 0
[gender] => male
)
)
如何按性别重新设置列表?
然后按“total = likes + comments + posts”
排序 总共= 10 性别=男性 总计= 8 性别=男性 总共10 性别=女性 总共= 7 性别=女性答案 0 :(得分:1)
请参阅array_multisort功能和示例
答案 1 :(得分:0)
几乎与您的previous question相同:
function cmp($a, $b) {
if ($a['gender'] == $b['gender']) {
$aSum = $a['likes'] + $a['comments'] + $a['posts'];
$bSum = $b['likes'] + $b['comments'] + $b['posts'];
if ($aSum == $bSum)
return 0;
return ($aSum < $bSum) ? -1 : 1;
}
return ($a['gender'] < $b['gender']) ? -1 : 1;
}
usort($array, "cmp");