-1

i am basically trying to make a leaderboard system. I need to put 2 types of data in my .yml file: the name of the player, and their score.

Here is a var dump:

array(3) {
  [0]=>
      array(2) {
        [0]=>
           string(10) "samueljh1_"
             [1]=>
                 int(3)
      }
      [1]=>
      array(2) {
        [0]=>
           string(12) "samueljh1_54"
             [1]=>
                 int(1)
      }
      [2]=>
      array(2) {
        [0]=>
           string(11) "samueljh1_1"
             [1]=>
                 int(8)
}

So, what I want to do is order this array, so that is is in numerical order - where the integers are.

Basically, converting the var dump above, to something like this:

array(3) {
  [0]=>
      array(2) {
        [0]=>
           string(11) "samueljh1_1"
             [1]=>
                 int(8)
      }
      array(2) {
        [1]=>
           string(10) "samueljh1_"
             [1]=>
                 int(3)
      }
      [2]=>
      array(2) {
        [0]=>
           string(12) "samueljh1_54"
             [1]=>
                 int(1)
      }

}

If this isn't possible, are there any alternate ways to store this data?

Thanks A lot, - Sam.

Samueljh1
  • 129
  • 1
  • 11
  • 1
    First of all i have to say i feel like your array structure is more complicated than it should be. In php there are tons of array functions including sorting, i try to say if you can make more light your array these [native functions](http://php.net/manual/en/array.sorting.php) will do the job – Santa's helper Apr 10 '15 at 13:12

1 Answers1

1

First, I suggest using the player name as an associative key for the score value and simplifying the structure of the array, like so:

$testArray = array("samueljh1_" => 3, "samueljh1_54" => 1, "samueljh1_1" => 8);

This makes the array much simpler to parse and makes the data structure more closely resemble the relationship between the data you have. Then the function arsort(), which does reverse sorting, is what you're looking for:

arsort($testArray, SORT_NUMERIC); // $testArray is passed by reference

var_dump($testArray);

Yields

array(3) {
  ["samueljh1_1"]=>
  int(8)
  ["samueljh1_"]=>
  int(3)
  ["samueljh1_54"]=>
  int(1)
}
Chris Brendel
  • 700
  • 3
  • 10
  • @Samueljh1 Updated this answer to use your sample data (and because originally I was sorting low-to-high). Let me know if this works for you! – Chris Brendel Apr 10 '15 at 13:31
  • Thats really helpful! I have one question though, i need to get the first 3 items in that array, - but not by the key name because this is stored somewhere else. Is there a way i can get the nth item in that array and get the name of the key? As its a leaderboard, i try to get the first value (like first place), once i have got the integer, how can i get the name of the key, that is holding that integer? I hope that makes sense! Cheers. – Samueljh1 Apr 10 '15 at 14:43
  • @Samueljh1 You could define a custom sort function to look into the nested array, like this: http://pastebin.com/x74VYYBM Should produce output like this: http://pastebin.com/YsJJxvS6 Then you can access, for example, the top scoring player's name like this: `$testArray[0]["player"]` Hope that helps you. :) – Chris Brendel Apr 10 '15 at 15:20
  • Ok Thanks! I'll see what i can do with that :) – Samueljh1 Apr 10 '15 at 15:27
  • Ok, i have some issues: because i am trying to do usort in a class, i need to do $this->usort() rather than just usort() . How can i do that in the string format? Thanks again! :) – Samueljh1 Apr 10 '15 at 16:16
  • Never mind, i got it! its just array(new ClassName(),"function") :) – Samueljh1 Apr 10 '15 at 16:17
  • Omg! Thanks so much! It Works!! Your a real life-saver! :D – Samueljh1 Apr 10 '15 at 16:22