从php中的对象数组获取所有最大值

时间:2020-10-06 09:36:48

标签: php arrays sorting object max

我有从mysql查询中获取的对象数组,我需要

  [
                {
                    "id": "11",
                    "from_userid": "1996",
                    "contest_id": "29",
                    "to_userid": "8",
                    "vote_date": "2020-10-06 01:40:04",
                    "count_votes": "1"
                },
                {
                    "id": "1",
                    "from_userid": "82",
                    "contest_id": "29",
                    "to_userid": "94",
                    "vote_date": "2020-09-03 07:06:36",
                    "count_votes": "1"
                },
               {
                    "id": "2",
                    "from_userid": "82",
                    "contest_id": "29",
                    "to_userid": "98",
                    "vote_date": "2020-09-03 07:06:36",
                    "count_votes": "0"
                }
            ]

我需要具有最高的“ count_votes”(例如id-id-11)和1个具有类似计数票数的对象。因此该函数应返回这两个对象。

我正在使用的函数仅返回一个对象。我需要两个对象,但可能需要最高(count_votes)个对象。 预期输出-

 [
                {
                    "id": "11",
                    "from_userid": "1996",
                    "contest_id": "29",
                    "to_userid": "8",
                    "vote_date": "2020-10-06 01:40:04",
                    "count_votes": "1"
                },
                {
                    "id": "1",
                    "from_userid": "82",
                    "contest_id": "29",
                    "to_userid": "94",
                    "vote_date": "2020-09-03 07:06:36",
                    "count_votes": "1"
                }
]

使用的功能-

function max_attribute_in_array($array, $prop) {
    return max(array_map(function($o) use($prop) {
                            return $o;
                         },
                         $array));
}

并且还尝试了此操作

function get_highest($arr) {
    $max = $arr[0]; // set the highest object to the first one in the array
    foreach($arr as $obj) { // loop through every object in the array
        $num = $obj['count_votes']; // get the number from the current object
        if($num > $max['count_votes']) { // If the number of the current object is greater than the maxs number:
            $max = $obj; // set the max to the current object
        }
    }
    return $max; // Loop is complete, so we have found our max and can return the max object
}

3 个答案:

答案 0 :(得分:1)

您可以使用array_column将所有count_votes值提取到一个数组中,然后可以使用其中的max

$max = max(array_column($arr, 'count_votes'));

然后您可以根据count_votes等于$max的值array_filter来数组:

$out = array_filter($arr, function ($o) use ($max) {
    return $o['count_votes'] == $max;
});

输出:

Array
(
    [0] => Array
        (
            [id] => 11
            [from_userid] => 1996
            [contest_id] => 29
            [to_userid] => 8
            [vote_date] => 2020-10-06 01:40:04
            [count_votes] => 1
        )
    [1] => Array
        (
            [id] => 1
            [from_userid] => 82
            [contest_id] => 29
            [to_userid] => 94
            [vote_date] => 2020-09-03 07:06:36
            [count_votes] => 1
        )
)

Demo on 3v4l.org

答案 1 :(得分:0)

注意 max与单级数组一起使用,因此所有对象都在内部转换为int。


@Nick指出,您的get_highest可以通过PHP函数完成:

function get_highest($array, $prop) {
    return max(array_column($array, $prop));
}

因此,您要做的就是通过此get_highest过滤数组:

$max = get_highest($myArray, 'count_votes');

$maxes = array_filter($myArray, fn($obj) => $obj['count_votes'] === $max);

答案 2 :(得分:0)

DF1
species  V1 V2
a
b
c
d
e

DF2

   a b d 
r1 . . .
r2 . . .
r3 . . .