I have this running in a while loop to add to the array every time it goes through it. However, my issue is I am trying to assign ranks for each value. Saw horse1
had AP=51
, EP=47
, SP= 32
, FX=20
. Horse2
had AP=52
, EP = 55
, SP=30
and F=19
. I am trying to make it print on the screen like so:
AP EP SP FX
Horse 1 2 2 1 1
Horse 2 1 1 2 2
And etc for however many horses there are.
Here is the code I have. I am not very well versed in PHP but I thought this was the way to go.
$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>
Assuming your horse array is like this,
$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);
OUTPUT:
Array
(
[1] => Array
(
[AP] => 2
[EP] => 2
[SP] => 1
[FX] => 1
)
[2] => Array
(
[AP] => 1
[EP] => 1
[SP] => 2
[FX] => 2
)
)