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
}