darbyp Posted May 17, 2015 Share Posted May 17, 2015 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 Quote Link to comment Share on other sites More sharing options...
darbyp Posted May 17, 2015 Author Share Posted May 17, 2015 also if possible if the value is over 0 then add onto a variable called $totalRides Quote Link to comment Share on other sites More sharing options...
Barand Posted May 17, 2015 Share Posted May 17, 2015 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 2 Quote Link to comment Share on other sites More sharing options...
darbyp Posted May 17, 2015 Author Share Posted May 17, 2015 thank you so much! I had to working but it looked so ugly.. Just there has learnt me a lot! might fall into another problem in a few minute haha (with another issue) Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted May 17, 2015 Share Posted May 17, 2015 This doesn't have to do with the code itself but the logic behind it. Doesn't matter if the round values higher than eight are similar? I was just wondering why add only eight highest. Quote Link to comment Share on other sites More sharing options...
darbyp Posted May 17, 2015 Author Share Posted May 17, 2015 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 Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted May 17, 2015 Share Posted May 17, 2015 array_combine() or array_merge() , depends what you need to do. Quote Link to comment Share on other sites More sharing options...
darbyp Posted May 18, 2015 Author Share Posted May 18, 2015 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted May 18, 2015 Share Posted May 18, 2015 Where are you getting the results from? Quote Link to comment Share on other sites More sharing options...
darbyp Posted May 18, 2015 Author Share Posted May 18, 2015 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted May 18, 2015 Share Posted May 18, 2015 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. Quote Link to comment Share on other sites More sharing options...
darbyp Posted May 18, 2015 Author Share Posted May 18, 2015 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted May 18, 2015 Share Posted May 18, 2015 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.