根据玩家ID从数组中找到最大的事件值

时间:2013-01-04 11:25:01

标签: php arrays multidimensional-array

我想通过玩家ID组找出数组中最大的event值。有两组玩家ID分别为10125和10132。

我有一个如下所示的数组:

Array
(
[0] => Array
    (
        [event] => 200
        [playerid] => 10125
        [session] => 23131885ccc560bb6c8ead02eb4919c1
    )

[1] => Array
    (
        [event] => 199
        [playerid] => 10125
        [session] => 23131885ccc560bb6c8ead02eb4919c1
    )

[2] => Array
    (
        [event] => 198
        [playerid] => 10125
        [session] => 23131885ccc560bb6c8ead02eb4919c1
    )

[3] => Array
    (
        [event] => 197
        [playerid] => 10125
        [session] => 23131885ccc560bb6c8ead02eb4919c1
    )

[4] => Array
    (
        [event] => 196
        [playerid] => 10125
        [session] => 23131885ccc560bb6c8ead02eb4919c1
    )

[5] => Array
    (
        [event] => 31
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[6] => Array
    (
        [event] => 30
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[7] => Array
    (
        [event] => 29
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[8] => Array
    (
        [event] => 28
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[9] => Array
    (
        [event] => 29
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[10] => Array
    (
        [event] => 28
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[11] => Array
    (
        [event] => 28
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[12] => Array
    (
        [event] => 27
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[13] => Array
    (
        [event] => 26
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[14] => Array
    (
        [event] => 25
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[15] => Array
    (
        [event] => 24
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[16] => Array
    (
        [event] => 23
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[17] => Array
    (
        [event] => 25
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[18] => Array
    (
        [event] => 24
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[19] => Array
    (
        [event] => 23
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[20] => Array
    (
        [event] => 22
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[21] => Array
    (
        [event] => 21
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[22] => Array
    (
        [event] => 20
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[23] => Array
    (
        [event] => 19
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[24] => Array
    (
        [event] => 18
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

[25] => Array
    (
        [event] => 1
        [playerid] => 10132
        [session] => 5a87375972ab4734e600c2dc1cec2ded
    )

)

结果如

[0] => Array
 (
    [event] => 200
    [playerid] => 10125
    [session] => 23131885ccc560bb6c8ead02eb4919c1
 )

[5] => Array
(
    [event] => 31
    [playerid] => 10132
    [session] => 5a87375972ab4734e600c2dc1cec2ded
)

1 个答案:

答案 0 :(得分:0)

您可以将数组从最高到最低排序,然后抓住第一个项目......

function cmp($a, $b)
{
    if ($a->playerId == $b->playerId) {
        return 0;
    }
    return ($a->playerId > $b->playerId) ? -1 : 1;
}

usort($array, "cmp");