0

Here is a situation:

race 1 = 7
race 2 = 3
race 3 = 1
race 4 = 2
race 5 = 6
race 6 = 2
race 7 = 7
race 8 = 3

The smaller the number the better since these are race positions. race number 1 MUST be added, regardless of it's magnitude and must be added to any 5 others that are selected on merit. So basically I want to use PHP to add up 6 of the best races out of the 8 and the 6 must include 1, regardless of whether it is among the best

I thoughtn of sorting the numbers by having them sorted from lowest to highest and adding the first 6. The problem is that if race 1, is not among the best 6, then this cannot work.

Any help will be appreciated, I am still thinking so I cannot provide anything in terms of what i have tried as everything is still at thought level!

4
  • 3
    try putting the numbers in an array, remove race 1, then sort and add. Commented Oct 25, 2011 at 0:57
  • make the associative array of race number as keys and position as values. Then you can sort the array by keys getting first 6 will always contain race 1 Commented Oct 25, 2011 at 1:01
  • Thanks. I am trying out Rob's suggestion but so far no luck. Sorting by keys may work on this one this one may work since it's race 1, the problem is that the criteria of which race is mandatory changes quite often, so it may nlot work as some races may use random names not even using numbers. I am hoping for some function! Thanks all the same. Commented Oct 25, 2011 at 1:10
  • @Bululu so you are saying you need the top 6 results AND a user-specified result, and the top 6 need to exclude the user-specified result? Commented Oct 25, 2011 at 1:13

2 Answers 2

1
<?php

    $race = array( 1 =>7, 2 => 3 );//etc
    $sum = $race[1];
    unset( $race[1] );

    sort( $race, SORT_NUMERIC );

    for( $i = 0; $i < 5; $i++ )$sum += array_pop( $race );
Sign up to request clarification or add additional context in comments.

3 Comments

Per this Question: stackoverflow.com/questions/369602/… you'll get some wonky behavior with pure numeric indexes - best to stick to the string indexing like my sample code.
Also, you are performing the array_pop a step too late - the required race can't be included in the top 6.
Thanks marabutt, I ried your code, it does not work, I am trying to modify it, so will post my results
0
<?php
/* if manually creating the array */
$race1 = 7;  
$races = array("race2" => 3, "race3" => 1, "race4" => 2); //...

/* if the array is created programmatically (preferred */
$race1 = $races[0];
$races = array_pop($races); //drops first element and resets the index 

/* then.... */

asort($races);

$total = $race1;
for($i=0; $i<6; $i++)
{
    $total += $races[$i]; 
}

?>

4 Comments

Summation can also be done with oneliner like: array_sum(array_slice($races, 0, 6))
Geting undefined offset 0 - undefined offset 5!
@Bululu, I only included 3 data points in my array. If you copy/pasted and didn't fill in the array with the rest of your data, it will fail after the 3 pass through the loop. If you did put them in and it is failing, look at the code you added - you may have accidentally created a sub-array or lumped a bunch of items into one node (likely race5 or race6).
I finished up the array. It is possible that I got something mixed up but from where I am seated it looks like I have used the code as intended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.