简体   繁体   English

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

[英]Get all max values from array of objects in php

I have this array of objects i'm getting from query in mysql, i need th我有这个对象数组,我从 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"
                }
            ]

I need the object which has highest 'count_votes ' for eg- id-11 and 1 have similar count votes.我需要对于 eg-id-11 和 1 具有最高 'count_votes' 的对象具有相似的计数投票。 So the function should return those 2 objects.所以函数应该返回这两个对象。

The function i am using returns only one object.我使用的函数只返回一个对象。 I need both of the objects whichever maybe but the highest(count_votes) objects.我需要两个对象,但可能是最高(count_votes)对象。 Expected Output-预期产出-

 [
                {
                    "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 used-使用的功能-

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

And tried this also-也试过这个——

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
}

You can use array_column to extract all the count_votes values into an array, which you can then take the max of:您可以使用array_column将所有count_votes值提取到一个数组中,然后您可以取max

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

You can then array_filter your array based on the count_votes value being equal to $max :然后,您可以array_filter基础上,你的阵列count_votes值等于$max

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

Output:输出:

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 3v4l.org 上的演示

NOTE max works with single level arrays, so all your objects are converted to int's internally.注意max适用于单级数组,因此您的所有对象都在内部转换为 int。


As @Nick has pointed out your get_highest can be done via PHP functions:正如@Nick 指出的那样,您的get_highest可以通过 PHP 函数完成:

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

So all you have to do is filter your array by this get_highest :所以你所要做的就是通过这个get_highest过滤你的数组:

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

$maxes = array_filter($myArray, fn($obj) => $obj['count_votes'] === $max);
function get_heighest($arr){
    $newArray = array();
    $voteCount = 0;

    foreach($arr as $obj){
        if($obj['count_votes'] >= $voteCount){
            array_push($newArray, $obj)
            $voteCount = $obj['count_votes'];
        }else{
            $i = 0;
            foreach($newArray as $object){
                if($object['count_votes'] < $voteCount){
                    unset($newArray[$i]);
                }
                $i++;
            }
        }
    }
    return $newArray;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM