-1

我让它在一个 while 循环中运行,以在每次通过它时添加到数组中。但是,我的问题是我正在尝试为每个值分配等级。看到horse1AP=51, EP=47, SP= 32, FX=20. Horse2AP=52,和. EP = 55_ 我试图让它像这样打印在屏幕上:SP=30F=19

          AP   EP   SP   FX  
Horse 1   2    2    1    1

Horse 2   1    1    2    2

等等,不管有多少马。

这是我的代码。我不是很精通PHP,但我认为这是要走的路。

$allstats[]= array
(
"AP"=>"x".$AP,
"EP"=>"x".$EP,
"SP"=>"x".$SP,
"FX"=>"x".$FX,
"Horse"=>$horse,
);

$APranks[$AP];
$EPranks[$EP];
$SPranks[$SP];
$FXranks[$FX];

ksort($APranks,2);
ksort($EPranks,2);
ksort($SPranks,2);
ksort($FXranks,2);
$FinalAP=(array_keys($APRanks,$AP));
?>
<div id="Rankings">
<? echo array_search($AP,$FinalAP);?><? echo array_search($EP,$EPranks);?><? echo         array_search($SP,$SPranks);?><? echo array_search($FX,$FXranks);?>
</div>
4

1 回答 1

0

假设你的马阵是这样的,

$horses[1]=array('AP'=>51,'EP'=>47,'SP'=>32,'FX'=>20);
$horses[2]=array('AP'=>52,'EP'=>55,'SP'=>30,'FX'=>19);

$horse_values = array('AP','EP','SP','FX');
$horse_rank = array();

foreach($horse_values as $k=>$val)
{
    uasort($horses, create_function('$a, $b', 'return custom_sort($a, $b, "'.$val.'");'));

    $i=1;
    foreach($horses as $l=>$horse)
    {
        $horse_rank[$l][$val] = $i;
        $i++;
    }
}

function custom_sort( $a, $b, $meta )  {
    if ( $a[$meta] == $b[$meta] )
        return 0;
    else if ( $a[$meta] > $b[$meta] )
        return -1;
    else
        return 1;
    //echo "$a, $b, $meta<hr>"; // Debugging output
}

arsort($horse_rank); //sorting the final rank array based on horse number
print_r($horse_rank);

输出:

Array
(
    [1] => Array
        (
            [AP] => 2
            [EP] => 2
            [SP] => 1
            [FX] => 1
        )
    [2] => Array
        (
            [AP] => 1
            [EP] => 1
            [SP] => 2
            [FX] => 2
        )
)
于 2013-09-04T13:28:45.513 回答