Hello,
I am trying to sort an array of integers by value (these are scores), and return an array with keys defined by their place in the original list (their team number), and element values equal to their sorted order (the team rank).
This I have accomplished with:
//OPEN AND FIX GLITCH
$filename = "scorestore.txt";
$score = file($filename);
for ($counter = 0; $counter < count($score); $counter +=1) {$score[$counter] = intval($score[$counter]);}
$scores=$score;
//APPEND TEAM NUMBER TO SCORE
for ($counter = 0; $counter < count($score); $counter +=1) {$hold = key($scores)+1; $scores[$counter] = $scores[$counter] . '.' . $hold; next($scores);}
//SORT
rsort($scores);
//REMOVE SCORE
for ($counter = 0; $counter < count($score); $counter +=1) {if (strlen(floor($scores[$counter]))==1) {$scores[$counter] = substr_replace($scores[$counter], '', 0, 2);}
else {$scores[$counter] = substr_replace($scores[$counter], '', 0, 3);}
}
//SWITCH to FORMAT: $scores[team#] = place
$scores = array_flip($scores);
Here's my problem:
I want to allow for teams (keys) with equal original values (scores) to have equal new values (ranking) equal to the lesser of the two. Basically, now that I have accomplished the first part, I want to go back, check for equal values, and for every group of equal elements, ignore the first, and subtract 1 from the value of the rest. I would loop this count($score) many times, to go through all possible equal values.
Every attempt I have made has failed in some way, such as only working for ties between adjacent numbers or subtracting from all involved teams rather than skipping over the first. I would welcome any help or solution, including one that discards the successful code I have written in favor of a new, working, overall solution.
the scorestore.txt will equal something like this:
3
3
4
10
1
5
9
10
10
Hopeful final output would be print_r($score); ==
Array ( [9] => 1 [5] => 1 [10] => 1 [8] => 4 [7] => 5 [4] => 6 [2] => 7 [1] => 7 [6] => 9 [3] => 10 )
Thank you for your time and help.