使用PHP添加一些数字


Adding some numbers using PHP

情况如下:

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

数字越小越好,因为这些是比赛位置。必须添加1号比赛,无论其大小如何,并且必须添加到根据成绩选择的其他5个比赛中。我想用PHP把8场比赛中最好的6场加起来6场比赛中必须包括1,不管它是不是最好的

我想通过将数字从低到高排序并将前6相加来对它们进行排序。问题是,如果种族1不是最好的6个,那么这就行不通了。

任何帮助都将不胜感激,我仍然在思考,所以我不能提供任何我所尝试过的东西,因为一切仍处于思想水平!

<?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 );
<?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]; 
}
?>