0

I have this array of objects i'm getting from query in mysql, i need th

  [
                {
                    "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. 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. 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
}

3 Answers 3

1

You can use array_column to extract all the count_votes values into an array, which you can then take the max of:

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

You can then array_filter your array based on the count_votes value being equal to $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

Sign up to request clarification or add additional context in comments.

Comments

0

NOTE max works with single level arrays, so all your objects are converted to int's internally.


As @Nick has pointed out your get_highest can be done via PHP functions:

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:

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

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

Comments

0
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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.