Im trying to sort largest number to smallest, and print the name of each 'stat' I cant figure it out. I got it to print the values of each field, but That is not needed. its also sorting least to greatest. can anyone please help? $sql="select physical,mental,position,timing,distance,momentum from users where id='$users[id]'"; $query=mysql_query($sql); $result=mysql_fetch_array($query); $statsInfo = array( 'Physical Balance' => $result['physical'], //6.550 'Mental Balance' => $result['mental'], //6.300 'Position' => $result['position'], //10.190 'Timing' => $result['timing'], //104.530 'Distance' => $result['distance'], //1.825 'Momentum' => $result['momentum'] //5.975 ); sort($statsInfo); foreach($statsInfo as $statName => $statValue) { print "$statsInfo[$statName]<br />"; } /* prints 1.825 5.975 6.300 6.550 10.190 104.530 When what i need is. the commented out is the value from above, only this time sorted. by name of stat, from greatest to least Timing //104.530 Position //10.190 Physical Balance //6.550 Mental Balance //6.300 Momentum //5.975 Distance //1.825 */ PHP: Ill even go with it printing least to greatest, I just want the name of the stat to print, not the value. I hope i was clear. Been driving me nuts. First experience with arrays
$statsInfo = array( 'Physical Balance' => $result['physical'], 'Mental Balance' => $result['mental'], 'Position' => $result['position'], 'Timing' => $result['timing'], 'Distance' => $result['distance'], 'Momentum' => $result['momentum'] ); sort($statsInfo); print_r(array_keys($statsInfo)) PHP: shows me: Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 ) i want it to print this: (the comments are hypothetical values so you can see its printing greatest to least as the name of the stat, not the value. im completely lost) Timing //104.530 Position //10.190 Physical Balance //6.550 Mental Balance //6.300 Momentum //5.975 Distance //1.825
$statsInfo = array("strength"=>4,"wisdom"=>4.3,"dexterity"=>2.2); arsort($statsInfo); foreach($statsInfo as $statName => $statValue) { echo $statName . "<br />"; } PHP: outputs: wisdom strength dexterity