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!

Bululu
  • 555
  • 2
  • 8
  • 14
  • 3
    try putting the numbers in an array, remove race 1, then sort and add. – Rob Allen Oct 25 '11 at 00: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 – Ehtesham Oct 25 '11 at 01: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. – Bululu Oct 25 '11 at 01: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? – Rob Allen Oct 25 '11 at 01:13

2 Answers2

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 );
Nick Maroulis
  • 486
  • 9
  • 28
  • 1
    Per this Question: http://stackoverflow.com/questions/369602/how-to-delete-an-element-from-an-array-in-php you'll get some wonky behavior with pure numeric indexes - best to stick to the string indexing like my sample code. – Rob Allen Oct 25 '11 at 01:15
  • Also, you are performing the array_pop a step too late - the required race can't be included in the top 6. – Rob Allen Oct 25 '11 at 01:27
  • Thanks marabutt, I ried your code, it does not work, I am trying to modify it, so will post my results – Bululu Oct 25 '11 at 01:32
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]; 
}

?>
Rob Allen
  • 17,381
  • 5
  • 52
  • 70
  • Summation can also be done with oneliner like: `array_sum(array_slice($races, 0, 6))` – zerkms Oct 25 '11 at 01:07
  • Geting undefined offset 0 - undefined offset 5! – Bululu Oct 25 '11 at 01:44
  • @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). – Rob Allen Oct 25 '11 at 10:44
  • 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. – Bululu Oct 25 '11 at 20:41