Jump to content

Need to sum up arrays, if the same mainkey appear different files


JalaVill
Go to solution Solved by JalaVill,

Recommended Posts

Hello!

 

I am new in PHP, and need some help.

 

I have two files: day1.txt and day2.txt

 

Each file have 20-100+ lines, and every line include the following information: FINISHING_PLACE:PLAYER_NAME:PLAYER_COUNTRY:PLAYER_POINTS:PERCENT. 

 

Example for day1.txt

202:Player3:Canada:566.35:0.047%
297:Player4:Canada:412.41:0.047%
318:Player1:Canada:366.39:0.047%
400:Player5:Canada:287.20:0.047%
403:Player2:Canada:154.43:0.047%
... 
etc

I receive similar file every day with the same data, but players position (finishing place) and player points are different. I want to combine all files, and make overall ranking. E.g. if PlayerX gets 200 points Day1. Then total he have 200 points. If next day he gets another 150 points, then his total points is 350 (200+150 = 350)

Why I need that? Its videogame, where lots of player, and need to make Canadian ranking. So, I made PHP array for Day1.txt, which looks like the following:

 

Array
(
    [Player3] => Array
        (
            [0] => 202
            [1] => Player3
            [2] => Canada
            [3] => 566.35
        )

    [Player4] => Array
        (
            [0] => 297
            [1] => Player4
            [2] => Canada
            [3] => 412.41
        )
...
etc. array for every player

So, my question is, how I can sum up specific player points from different days (files)? I need it because I want to make overall ranking. Actually players have real name, but to protect innocent and make easier sample, replaced names by Player1, Player2 etc.

Link to comment
Share on other sites

Dump all that data into a database, then you do a query to get whatever you need from it. It'd look something like

SELECT SUM(points) FROM table WHERE player = "Player1" AND date BETWEEN "starting date" AND "ending date"
The "date" would be what the file corresponds to, or maybe when you import it.
Link to comment
Share on other sites

  • Solution

Dump all that data into a database, then you do a query to get whatever you need from it. It'd look something like

SELECT SUM(points) FROM table WHERE player = "Player1" AND date BETWEEN "starting date" AND "ending date"
The "date" would be what the file corresponds to, or maybe when you import it.

 

 

Sorry for delayd reply, but your comment was very helpful, and now script is ready.

 

Again, thank you very much.

 

EDIT:

 

Just in case I add my code line, if someone have similar problem and find this thread through google.

 

$query = "SELECT player, country, SUM(points), COUNT(player) FROM YOUR_TABLE GROUP BY player ORDER BY SUM(points) DESC"; 

Edited by JalaVill
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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