JasonO Posted March 30, 2008 Share Posted March 30, 2008 Hi guys, I have an array which I believe is in another array (I totally suck with arrays, still learning). I query a game server for the server's information and it brings back something like this Array ( [arma] => Array ( [num_players] => 13 [players] => Array ( [0] => Array ( [player] => -{GOL}-Bogart [team] => Pvt [score] => 144 [deaths] => 11 ) [1] => Array ( [player] => Jazzzzz [team] => [score] => 44 [deaths] => 16 ) [2] => Array ( [player] => -{GOL}-Matti [team] => PFC [score] => 242 [deaths] => 5 ) [3] => Array ( [player] => rv [team] => [score] => 22 [deaths] => 15 ) [4] => Array ( [player] => Hylten [team] => [score] => 64 [deaths] => 4 ) [5] => Array ( [player] => [KS]4RC|Tpr.Bauer [team] => [score] => 26 [deaths] => 9 ) ) [num_teams] => 0 ) ) Now, each player has a score. I want to order the array so each player is ordered with the highest scoring player comes first. I've tried the sort() and ksort() stuff found on different sites going through Google but with no luck. I do my best trying to teach myself a lot of things, but there's always the odd thing I spend a while looking at and searching and searching and not get anywhere. Here is my last resort. Thanks guys (and girlz if they come across this post ) Jason Link to comment https://forums.phpfreaks.com/topic/98623-sorting-by-a-particlar-value-in-an-array/ Share on other sites More sharing options...
tibberous Posted March 30, 2008 Share Posted March 30, 2008 Ascending or decending? Link to comment https://forums.phpfreaks.com/topic/98623-sorting-by-a-particlar-value-in-an-array/#findComment-504720 Share on other sites More sharing options...
tibberous Posted March 30, 2008 Share Posted March 30, 2008 Try this: function highest($a, $b){ return $a['score'] > $b['score']; } usort($g4y['arma']['players'], 'highest'); Replace $g4y with the name of your array. Link to comment https://forums.phpfreaks.com/topic/98623-sorting-by-a-particlar-value-in-an-array/#findComment-504725 Share on other sites More sharing options...
JasonO Posted March 30, 2008 Author Share Posted March 30, 2008 Hi, That appears to work.. but shows lowest first so I need it the other way around. I will try playing with that function and see what happens. Thanks for your help though, greatly appreciated. Nope got it with: function highest($b, $a){ return $a['score'] > $b['score']; } usort($results['arma']['players'], 'highest'); So, result: PS - Solve button still missing? Link to comment https://forums.phpfreaks.com/topic/98623-sorting-by-a-particlar-value-in-an-array/#findComment-504753 Share on other sites More sharing options...
moon 111 Posted March 30, 2008 Share Posted March 30, 2008 function highest($a, $b){ return $a['score'] < $b['score']; } usort($g4y['arma']['players'], 'highest'); Will do it the other way around Link to comment https://forums.phpfreaks.com/topic/98623-sorting-by-a-particlar-value-in-an-array/#findComment-504756 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.