首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从php的对象数组中获取所有最大值

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

Stack Overflow用户
提问于 2020-10-06 17:36:48
回答 3查看 147关注 0票数 0

我在mysql中从query中获得了这个对象数组,我需要

代码语言:javascript
复制
  [
                {
                    "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-11和1有类似的计数投票。所以函数应该返回这两个对象。

我使用的函数只返回一个对象。我需要这两个对象,除了最高的(Count_votes)对象。预期输出-

代码语言:javascript
复制
 [
                {
                    "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"
                }
]

使用的函数-

代码语言:javascript
复制
function max_attribute_in_array($array, $prop) {
    return max(array_map(function($o) use($prop) {
                            return $o;
                         },
                         $array));
}

我也试过了-

代码语言:javascript
复制
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
}
EN

回答 3

Stack Overflow用户

发布于 2020-10-06 17:40:35

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

代码语言:javascript
复制
$max = max(array_column($arr, 'count_votes'));

然后,可以根据等于$maxcount_votes值对数组执行array_filter操作

代码语言:javascript
复制
$out = array_filter($arr, function ($o) use ($max) {
    return $o['count_votes'] == $max;
});

输出:

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2020-10-06 17:41:59

max与单级数组一起工作,因此所有对象都会在内部转换为整数。

正如@Nick所指出的,你的get_highest可以通过PHP函数来实现:

代码语言:javascript
复制
function get_highest($array, $prop) {
    return max(array_column($array, $prop));
}

因此,您所要做的就是根据此get_highest过滤数组

代码语言:javascript
复制
$max = get_highest($myArray, 'count_votes');

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

Stack Overflow用户

发布于 2020-10-06 18:26:49

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64223167

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档