Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

How to sum up certain array values, if main key the same

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

How to sum up certain array values, if main key the same

Postby JalaVill » Tue Sep 10, 2013 5:03 am

New in PHP

I have two files named as day1.txt and day2.txt

Example for file day1.txt:
Code: Select all
202:Player1:Male:566.35:0.047%
223:Player2:Canada:542.75:0.045%
252:Player3:Canada:519.15:0.043%
453:Player4:Canada:365.76:0.03%
541:Player5:Canada:342.17:0.028%
551:Player6:Canada:342.17:0.028%
850:Player7:Canada:271.37:0.022%
... etc.


and day2.txt
Code: Select all
202:Player5:Male:566.35:0.047%
223:Player3:Canada:542.75:0.045%
252:Player1:Canada:519.15:0.043%
453:Player7:Canada:365.76:0.03%
541:Player2:Canada:342.17:0.028%
551:Player4:Canada:342.17:0.028%
850:Player6:Canada:271.37:0.022%
... etc


To make things more clear - its a videogame. Every day one event occur (duel), and it ranks all players if event is finished. On the file definitions for every word is: FINISHING_PLACE:PLAYER_NAME:COUNTRY:GOT_POINTS:TOTAL_POINTS%

Every day I receive a new file, and now I want to combine these two rankings into one.

E.g. if Player_X got 100 points on Day1, and 150 points on Day2, then Player_X total points should be 250. No need to sum up another, only player_points.
I couldnt figure out, how I can do that.

I made an array ([3] is player_points)

Code: Select all
Array
(
    [Player1] => Array
        (
            [0] => 202
            [1] => player1
            [2] => Canada
            [3] => 566.35
            [4] => 0.047
        )

    [Player2] => Array
        (
            [0] => 1504
            [1] => Player2
            [2] => Canada
            [3] => 566.35
            [4] => 0.045
        )
etc ....


Now, how I can add values together, if the same player is found another file?
JalaVill
New php-forum User
New php-forum User
 
Posts: 1
Joined: Tue Sep 10, 2013 4:41 am

Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot], yetticaretti and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.