DanielBP Posted September 14, 2011 Share Posted September 14, 2011 Part of my code: <?php $arr = array ("Christina", "Daniel", "Andreea); reset($arr); foreach ($arr as $username) { //more code here ... foreach ($stats as $result) { echo $username."'s points - ".$result."<br />"; } } ?> And gives this: Christina's points - 14 Daniel's points - 45 Andreea's points 23 My question is: can you sort the the $result so it will show Daniel's points - 45 Andreea's points 23 Christina's points - 14 Thank you Quote Link to comment https://forums.phpfreaks.com/topic/247140-sorting/ Share on other sites More sharing options...
AyKay47 Posted September 14, 2011 Share Posted September 14, 2011 depending on the rest of your code.. you can probably use sort to sort the $result array in the order that you want it. Quote Link to comment https://forums.phpfreaks.com/topic/247140-sorting/#findComment-1269295 Share on other sites More sharing options...
ManiacDan Posted September 14, 2011 Share Posted September 14, 2011 You have to build an array of username=>score and then use ksort to sort by keys. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/247140-sorting/#findComment-1269322 Share on other sites More sharing options...
Psycho Posted September 14, 2011 Share Posted September 14, 2011 You have to build an array of username=>score and then use ksort to sort by keys. To quote someone else from today "I hate to turn your posts back at you, but . . ." His example shows the scores sorted in reverse order. If the scores are the values then the function to use would arsort() which will sort the values in reverse order and maintain index association. Quote Link to comment https://forums.phpfreaks.com/topic/247140-sorting/#findComment-1269325 Share on other sites More sharing options...
DanielBP Posted September 14, 2011 Author Share Posted September 14, 2011 Well I did tried to use the sort functions in php but I don't know how so I found an alternative to it by using a javascript (this) and it works <table class="sortable"> <tr> <th>Username</th> <th>Points</th> </tr> <?php $arr = array ("Christina", "Daniel", "Andreea"); reset($arr); foreach ($arr as $username) { //more code here ... foreach ($stats as $result) { echo "<tr><td>".$username."</td><td>".$result."</td></tr>"; } } ?> </table> The only problem is, the user HAS to click the column to sort the results, it won't do it automatically. Bahhh, I'm such a mess at coding Quote Link to comment https://forums.phpfreaks.com/topic/247140-sorting/#findComment-1269335 Share on other sites More sharing options...
Pikachu2000 Posted September 14, 2011 Share Posted September 14, 2011 Well, maybe if you'd post all of the relevant code, someone could suggest a less complicated way to do it. Quote Link to comment https://forums.phpfreaks.com/topic/247140-sorting/#findComment-1269366 Share on other sites More sharing options...
AyKay47 Posted September 15, 2011 Share Posted September 15, 2011 You have to build an array of username=>score and then use ksort to sort by keys. To quote someone else from today "I hate to turn your posts back at you, but . . ." His example shows the scores sorted in reverse order. If the scores are the values then the function to use would arsort() which will sort the values in reverse order and maintain index association. however this might be an example array.. we are not sure if the array will be in reverse order everytime.. I like choosing the order manually here, however if the array is always in reverse order.. then yes arsort() would be a good option.. Quote Link to comment https://forums.phpfreaks.com/topic/247140-sorting/#findComment-1269574 Share on other sites More sharing options...
Psycho Posted September 15, 2011 Share Posted September 15, 2011 You have to build an array of username=>score and then use ksort to sort by keys. To quote someone else from today "I hate to turn your posts back at you, but . . ." His example shows the scores sorted in reverse order. If the scores are the values then the function to use would arsort() which will sort the values in reverse order and maintain index association. however this might be an example array.. we are not sure if the array will be in reverse order everytime.. I like choosing the order manually here, however if the array is always in reverse order.. then yes arsort() would be a good option.. The OP gave an example of how the records were to be sorted. The only thing that is sorted in that example is the score - in reverse order. I am not going to make wild conclusions based on information that the OP did not provide. Even, ManiacDan had suggested using a reverse sort, he just goofed and provided a function that would sort by the name instead of the score, which is clearly not what the OP is looking for. Quote Link to comment https://forums.phpfreaks.com/topic/247140-sorting/#findComment-1269630 Share on other sites More sharing options...
AyKay47 Posted September 15, 2011 Share Posted September 15, 2011 You have to build an array of username=>score and then use ksort to sort by keys. To quote someone else from today "I hate to turn your posts back at you, but . . ." His example shows the scores sorted in reverse order. If the scores are the values then the function to use would arsort() which will sort the values in reverse order and maintain index association. however this might be an example array.. we are not sure if the array will be in reverse order everytime.. I like choosing the order manually here, however if the array is always in reverse order.. then yes arsort() would be a good option.. The OP gave an example of how the records were to be sorted. The only thing that is sorted in that example is the score - in reverse order. I am not going to make wild conclusions based on information that the OP did not provide. Even, ManiacDan had suggested using a reverse sort, he just goofed and provided a function that would sort by the name instead of the score, which is clearly not what the OP is looking for. going off of strictly with what the OP provided, yes the solution given would be the best way to approach this Quote Link to comment https://forums.phpfreaks.com/topic/247140-sorting/#findComment-1269660 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.