0

I'm storing in a multi-dimensional array the progressive score of n players in different days. So I've something like

Array
(
    [0] => Array
    (
        [day] => 2014-10-01
        [player] => John
        [score] => 1500
    )
    [1] => Array
    (
        [day] => 2014-10-02
        [player] => John
        [score] => 1510
    )
    [2] => Array
    (
        [day] => 2014-10-01
        [player] => Mary
        [score] => 1400
    )
    [3] => Array
    (
        [day] => 2014-10-02
        [player] => Mary
        [score] => 1600
    )
)

What I need a day-by-day rank for a given player, comparing his score with all other players's one, for each day. So the input will be player's id (in this case "John") and the output should be a JSON object like

[{"day"="2014-10-01", "rank": 1}, {"day"="2014-10-02", "rank": 2}]

What's the best way to approach this problem? To give you an idea of the dimension of my data, consider that I've the scores of 100 players in 100 different days (so I've a table of approximately 10.000 records).

EDIT (27/11/14): the multi-dimensional array is the result of a pre-processing on data stored in a MySQL database. This database contains information about each game played in this form:

|Date      |WinnerId|LoserId|PtsEarned|PtsLost|
|2014-10-01|John    |Mary   |10       |-10    |
|2014-10-02|Mary    |John   |20       |-20    |

So I don't know If I can easily use the method suggested in the "duplicate" answer. With my first approach, it takes about 7-8 seconds to output the result I need... so I think It's not the most efficient way to face the problem...

EDIT (28/11/14): I tried ranking data directly in MySql, and it takes about 2 seconds to give me back the ranking position of one player in a specific day; repeating the same query 100 times is a way too long process... With a different approach I manage to create a php array with "pre-processed" data in the same time (2 seconds), but I miss the last step, i.e. how to do a rank using php functions on a multi-dimensional array (my first question). Any idea?

Community
  • 1
  • 1
Hunter
  • 310
  • 1
  • 5
  • 15
  • If it's in a database table, can you do your filtering there with sql? Using group by on date and player and ordering by rank? – vch Nov 27 '14 at 17:19
  • A... database? Also, 10.000 is only a big number to humans; computers can churn through that in no time. – Sverri M. Olsen Nov 27 '14 at 17:19
  • I've added some information about my database structure; hope it can help to better understand my problem – Hunter Nov 27 '14 at 18:39
  • You could keep a record (database) of all the top one hundred players at the end of each day. It is fixed, not too big, and a simple lookup to display it. You keep an archive of how many days that you want. If someone wants their rating who is not in the top 100 then they wait 10 seconds for the results. The cost is some disk space that is cheap. – Ryan Vincent Nov 28 '14 at 19:02
  • For today you just keep a record of the top 100. If the current user score is in the range then insert it and display the list. Is really quick and cheap. – Ryan Vincent Nov 28 '14 at 19:04

0 Answers0