0

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

Andrew Walker
  • 462
  • 5
  • 22
  • you are not giving enough information. please provide more code. your usort call will do nothing since it doesn't know ehat $rempArr[][] is. you are supposed to pass an array to it – vlad katz May 09 '21 at 11:08
  • @Andrew this is not a clear [mcve]. I don't see the need for `$i` -- just use an empty set of square braces. To preserve the names after sorting use `uasort()`. The double empty braces in your sorting call is nonsense and exposes that you have not researched enough. – mickmackusa May 09 '21 at 13:17
  • There are many duplicates on this topic. That said, maybe add all values, subtract the `max()` value, then divide by 3, right? I mean, you don't even need to sort, right? – mickmackusa May 09 '21 at 13:22
  • Hello, sorry for the delay. @vladkatz The `$tempArr[][]` should provide an array, as it is sorting an array with those keys. So the hope was it would sort them. @mickmackusa The $i is because I will want to reuse this array marking the Round number, As there is not a Round 0, I wanted to start it as 1, `[]` still start at 0. because the coming in is what I would call messey, it was was the easy option. But I will try your `max()` function. – Andrew Walker May 09 '21 at 17:13
  • @Andrew you need to provide a clear, yet minimal set of sample data. We don't see where `$i` is magically declared. Show us a small sample of your scraped data, then show us your EXACT desired output for that input. We also want to see how far you got on your own because if you have already solved 75% of the task, then we only need to show you the remaining 25%. So far, I don't see any compelling reason to sort the users' data. (p.s. you can only ping one user per comment -- I was 2nd so I was not pinged.) – mickmackusa May 09 '21 at 23:30
  • @mickmackusa Full function and Sample Data added. – Andrew Walker May 10 '21 at 14:18
  • Add your desired output... – Kevin Gales May 10 '21 at 21:56
  • @AndrewWalker there is no EXACT desired output -- as I already mentioned. I don't know if this is suitable for your task. https://3v4l.org/NgHWu I looks like you are not researching between edits. We don't need your question on Stack Overflow -- we already have loads of sorting questions here. Try to understand your own needs and self-solve. – mickmackusa May 10 '21 at 22:05
  • @mickmackusa I apricate your view, but I will put my self-solved function up. I cannot find, including your and others links, looks little like mine. :) I do accept I could have ask it better in hindsight :) – Andrew Walker May 11 '21 at 13:58
  • Just realised I cannot because my post has been closed. Shame because my code to solve this is nothing like I have seen, based on my array which is not simple. – Andrew Walker May 11 '21 at 14:02
  • @Andrew I'll be happy to review your solution if you want to mention a 3v4l.org demo. The truth is that your question is a multi-question task and not a great fit for Stack Overflow. You are asking how to 1. Loop then 2. Sort 3. Remove the high element 4. Find the average. Stack Overflow has pre-existing pages dedicated to each of these tasks. I see the `usort()` problem as the standout problem in your question. You will not find any demos/tutorials any where that use those double square braces. – mickmackusa May 11 '21 at 21:58

0 Answers0