Jump to content

get highest values


darbyp

Recommended Posts

Hello all,

Just wondering if someone could help me.

 

my array looks like this:

"riderName" => "Example Rider",
"round1" => "20",
"round2" => "17",
"round3" => "18",
"round4" => "19",
"round5" => "16",
"round6" => "20",
"round7" => "18",
"round8" => "10",
"round9" => "17",
...
"round22" => "18",

is there anyway i can loop through the rounds and get the 8 highest values and add them together to a variable?

 

Thank you

 

Link to comment
Share on other sites

Why does your array look like

$array = [
    "riderName" => "Example Rider",
    "round1" => "20",
    "round2" => "17",
    "round3" => "18",
    "round4" => "19",
    "round5" => "16",
    "round6" => "20",
    "round7" => "18",
    "round8" => "10",
    "round9" => "17",
    "round10" => "0",
    "round11" => "0",
    "round22" => "18"
];

When it would make more sense to be like

$array = [
    "Example Rider" => [
                         "20",
                         "17",
                         "18",
                         "19",
                         "16",
                         "20",
                         "18",
                         "10",
                         "17",
                         "0",
                         "0",
                         "18"
                        ]
];

Then all that is required is

foreach ($array as $rider => $rounds) {
    $totalRides = count(array_filter($rounds));  // count non-zero values
    rsort($rounds);
    $totalHigh8 = array_sum(array_slice($rounds,0,);  // get sum of highest 8
    
    echo "Rider: $rider<br>Total rides: $totalRides<br>Highest 8: $totalHigh8<br><br>";
}

giving

Rider: Example Rider
Total rides: 10
Highest 8: 147

  • Like 2
Link to comment
Share on other sites

Only the highest 8 because the cycling club hold a race every Wednesday and 20 points goes to the fast club rider all the way down to 1. The maximum points is 160 so it is based off the 8 best races if two riders on 160 then fastest time :).

 

Now to merge both arrays or link somehow to pull in fastest time.

 

Defo past my experience but guess I can find out

Link to comment
Share on other sites

Pretty much I want my results array to be...

 

"date of race" => "ridername","club","time"

. "ridername","club","time"

. "ridername","club","time"

 

But this on multiple dates and different clubs.

 

I was hoping there would be away of going down the list of each dates and when it sees the first person from my club give them 20, second 19 etc etc but then start on the next date.

 

And calculate it similar to the top?

 

This possible?

 

Thanks

Link to comment
Share on other sites

From the race results I am manually putting them in the array. So one week there could be 50 people next week could be 12.

 

They will always be "name","club","time" but the date of the race will always be different

Link to comment
Share on other sites

Instead of changing your program every week it would be much better to put your data into a database table each week

CREATE TABLE club (
  `club_id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `clubname` varchar(50)
);

CREATE TABLE rider (
  `rider_id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `club_id int,
  `ridername` varchar(50),
  KEY `idx_club` (`club_id`),
);

CREATE TABLE `race_time` (
  `race_time_id` int(11) NOT NULL AUTO_INCREMENT,
  `race_date` date DEFAULT NULL,
  `rider_id` int DEFAULT NULL,
  `club_id` int DEFAULT NULL,
  `time` time DEFAULT NULL,
  PRIMARY KEY (`race_time_id`),
  KEY `idx_club` (`club_id`),
  KEY `idx_rider` (`rider_id`)
);

You can then either

  • use SQL queries to calculate postions, points etc, or
  • load your arrays from the database and calculate

 

If you don't like the idea of a database then at least store your results in a text file, appending new results each week and load your arrays from there. Or use a spreadsheet and export a csv each week.

 

Whichever way you go, you shouldn't be continually changing the program code.

Link to comment
Share on other sites

the code shouldn't change but the data in the array.

 

it will be 

"18/05/2015" with "name","club","time"

"10/05/2015" with "name",club","time"

etc etc if that helps?

 

might be shooting the dark here and will have to just start making a database

Link to comment
Share on other sites

The processing you want to do may involve sorting by dates. The date format you are using cannot be sorted, Always use date format of YYYY-MM-DD in data. You can always reformat it for presentation.

 

As I have demonstrated already, some array structures are more useful than others for certain tasks. Another advantage of external data is that you can filter the data when loading and load into a suitable array structure for the task.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.