Sort your data by values : https://laravel.com/docs/5.7/collections#method-sort And then you can loop trough it, and implement a counter for the ranking.
Sep 14, 2018
18
Level 4
Students Position From Average
Please i have a data like this..
array:9 [
3101625668 => 98.0,
3101635921 => 98.0,
3913126364 => 35.77,
3913058204 => 25.33,
3101372540 => 33.47,
3913752741 => 40.0,
3913120054 => 20.4,
3913998755 => 26.67,
3913861492 => 25.2,
3913881854 => 19.8,
]
I want to get the students position from the average. The array keys represent the student PIN Number and the value is their average. In array[0] and array[1], the values are the same and i want to get something like "1st" twice then "3rd"...
Someone help me please, have been awake all night working on this...
@andreasbakir thanks for the previous help you rendered! I appreciate man!
I really need help with this one!
Level 20
$array_results = [3101625668 => 98.0, 3101635921 => 98.0, 3913126364 => 35.77, 3913058204 => 25.33, 3101372540 => 33.47, 3913752741 => 40.0, 3913120054 => 20.4, 3913998755 => 26.67, 3913861492 => 25.2, 3913881854 => 19.8,];
$results = collect($array_results);
$results_sorted = $results->sort()->reverse();
$current_rank = 1;
$number_in_position = 0;
$max_score = 100;
$current_score = $max_score;
foreach($results_sorted as $pin => $result)
{
if($result < $current_score ){
$current_score = $result;
$current_rank += $number_in_position;
$number_in_position = 1;
$output[$pin] = [$current_rank, $result];
}else{ //same score as the previous
$number_in_position++;
$output[$pin] = [$current_rank, $result];
}
}
dd($output);
1 like
Please or to participate in this conversation.