Jump to content

assigning rank to array keys


Cjones

Recommended Posts

${'stats' . $run} = array
  (
  "AP"=>$AP,
  "EP"=>$EP,
  "SP"=>$SP,
  "FX"=>$FX,
  "Horse"=>$horse,
  
  );
  $allstats[] = ${'stats' . $run};

I have that running in a while loop to add an array to the main one every time it goes through it. However, my issue is I am trying to assign ranks for each value. Saw horse 1 had AP=51 , EP=47, SP= 32, FX=20. Horse 2 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.

 

Any ideas or help would be much appreciated. Thanks

Link to comment
Share on other sites

${'stats' . $run} = array
No. Just, no. Use a regular array. You can probably even do away with the whole $run and ${stats} stuff.

$allstats[]= array
  (
  "AP"=>$AP,
  "EP"=>$EP,
  "SP"=>$SP,
  "FX"=>$FX,
  "Horse"=>$horse,
  
  );
As for the ranking,

 

You'll need to know what values there are in each category. Build an array for each of the four, containing each horse's score as the key to the array (to eliminate duplicates).

// such as
$APranks[$AP] = 1; // value doesn't matter
When you're done, ksort each array in whichever order then grab just the keys. Now you have an array that looks like

$APranks = array( // higher is better in this example
    0 => 100,
    1 => 99,
    2 => 95,
    3 => 90, ...
)
The rank would be whatever the corresponding key is + 1, so the rank for an AP=95 is 3. As you're printing out each horse you can get that "corresponding key" with array_search.
Link to comment
Share on other sites

I see. Only issue with the knowing each value for each category the numbers can be 54.27 or 62.14. They go to the tenths so it wouldn't be very feasible for me to type in every  number for the ranking. Is it possible to sort them and whatever number is higher gets 1 or so on?

Link to comment
Share on other sites

You don't have to type in anything. It happens automatically.

 

But you're dealing with non-whole numbers? Can't have decimal numbers as array keys. You can get around that: make each array key a string by making it non-numeric, like "x54.27". You don't have to get the original numbers back out from that string so it's not so bad.

If that feels weird, add them all to the array as values (not keys), then use array_unique() to remove duplicates and array_values() to reindex.

Edited by requinix
Link to comment
Share on other sites

My PHP isn't the strongest as I can usually just get myself in trouble. However, I am having some issues and I know I am completely butchering this code. Here is what I have from what you said and it isn't working (user error). Can you give me some more pointers.

$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));
  $FinalEP=(array_keys($EPRanks,$EP));
  $FinalSP=(array_keys($SPRanks,$SP));
  $FinalFX=(array_keys($FXRanks,$FX));
  ?>
  <div id="Rankings">
<? echo array_search($AP,$FinalAP);?><? echo array_search($EP,$FinalEP);?><? echo array_search($SP,$FinalSP);?><? echo array_search($FX,$FinalEX);?>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.