数组按性别排序

时间:2014-01-17 14:35:12

标签: php arrays sorting

我的列表数组

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 性别=女性

1 个答案:

答案 0 :(得分:1)

使用usort()

function cmp($a, $b) {
    if ($a['gender'] == $b['gender']) {
        return 0;
    }
    return ($a['gender'] < $b['gender']) ? -1 : 1;
}

usort($array, "cmp");