Cjones Posted September 4, 2013 Share Posted September 4, 2013 ${'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 Quote Link to comment https://forums.phpfreaks.com/topic/281831-assigning-rank-to-array-keys/ Share on other sites More sharing options...
requinix Posted September 4, 2013 Share Posted September 4, 2013 ${'stats' . $run} = arrayNo. 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 matterWhen 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. Quote Link to comment https://forums.phpfreaks.com/topic/281831-assigning-rank-to-array-keys/#findComment-1448058 Share on other sites More sharing options...
Cjones Posted September 4, 2013 Author Share Posted September 4, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/281831-assigning-rank-to-array-keys/#findComment-1448062 Share on other sites More sharing options...
requinix Posted September 4, 2013 Share Posted September 4, 2013 (edited) 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 September 4, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/281831-assigning-rank-to-array-keys/#findComment-1448063 Share on other sites More sharing options...
Cjones Posted September 4, 2013 Author Share Posted September 4, 2013 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);?> Quote Link to comment https://forums.phpfreaks.com/topic/281831-assigning-rank-to-array-keys/#findComment-1448069 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.