I am not sure if there is a better way of doing this, but I am stuck either way.
I am getting results from a website (using web scraping) and sorting the data. The data consists of a User, and their time recorded flying a track over a number of rounds.
To put this into an array I have used this:
function sortRes($data) {
//echo json_encode($data);
$arr = array('Division' => $_GET['div']);
$tempArr = array();
$i = 1; // Round number
foreach ($data as $res) {
$tempArr[$res['name']][$i] = $res['time'];
$i++;
}
usort($tempArr[][], function($a, $b) {
return $a - $b;
});
print_r($tempArr['Rich']);
}
Here is the sample data:
[{"pos":"1","name":"Rich","time":"142.988","points":"8"},{"pos":"1","name":"Rich","time":"144.153","points":"8"},{"pos":"1","name":"Rich","time":"145.307","points":"8"},{"pos":"1","name":"Rich","time":"144.834","points":"8"},{"pos":"2","name":"Phobos","time":"152.261","points":"7"},{"pos":"2","name":"Markim","time":"150.261","points":"7"},{"pos":"2","name":"Markim","time":"149.542","points":"7"},{"pos":"2","name":"Phobos","time":"152.261","points":"7"},{"pos":"3","name":"Markim","time":"157.045","points":"6"},{"pos":"3","name":"Phobos","time":"152.069","points":"6"},{"pos":"3","name":"Phobos","time":"151.047","points":"6"},{"pos":"3","name":"Markim","time":"157.045","points":"6"},{"pos":"4","name":"Tall-E FPV Brett","time":"179.923","points":"5"},{"pos":"4","name":"Tall-E FPV Brett","time":"191.926","points":"5"},{"pos":"4","name":"Tall-E FPV Brett","time":"179.902","points":"5"},{"pos":"4","name":"Tall-E FPV Brett","time":"179.923","points":"5"}]
So to print a users times I can use print_r($tempArr);
And this gives me this result:
Array ( [Rich] => Array ( [1] => 142.988 [2] => 144.153 [3] => 145.307 [4] => 144.834 ) [Phobos] => Array ( [5] => 152.261 [8] => 152.261 [10] => 152.069 [11] => 151.047 ) [Markim] => Array ( [6] => 150.261 [7] => 149.542 [9] => 157.045 [12] => 157.045 ) [Tall-E FPV Brett] => Array ( [13] => 179.923 [14] => 191.926 [15] => 179.902 [16] => 179.923 ) [0] => Array ( [0] => ) )
So the key for the array is the users name, taking from the website.
What I need to do, is find the highest number and remove it, and then average the remaining 3 times, and create a new array with the users name, and average time. And with all the users sort it lowest time first. So that is the goal.
I could do a condition, but I thought it would be quicker to sort the times, lowest to highest, and remove the last array (round number) in my case would be [4].
I am unable to get to the times to sort them. How do I get inside the array?
This is my attempt, and I get no errors, but no sorting either:
usort($tempArr[][], function($a, $b) {
return $a - $b;
});
Any pointers on how to do this with the least CPU time as possible ?
Many thanks