Jump to content

Sorting by a particlar value in an array


JasonO

Recommended Posts

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 :P)

 

Jason

Link to comment
https://forums.phpfreaks.com/topic/98623-sorting-by-a-particlar-value-in-an-array/
Share on other sites

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:

 

image.php

 

PS - Solve button still missing?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.