You mentioned that this array is the result of a database query. Therefore, you should not repeat these results, so you should focus on how you get these results from your database, since SQL can do all this math for you with better performance.
To show this, imagine that your database table is named my_table and contains all the information you specified above: ( source_name , week , picks , won , lost , draw ):
+-------------+------+-------+-----+------+------+ | source_name | week | picks | won | lost | draw | +-------------+------+-------+-----+------+------+ | A | 10 | 1 | 0 | 1 | 0 | +-------------+------+-------+-----+------+------+ | A | 10 | 1 | 1 | 0 | 0 | +-------------+------+-------+-----+------+------+ | A | 11 | 1 | 1 | 0 | 0 | +-------------+------+-------+-----+------+------+ | A | 11 | 1 | 1 | 0 | 0 | +-------------+------+-------+-----+------+------+ | A | 11 | 1 | 0 | 1 | 0 | +-------------+------+-------+-----+------+------+ | A | 11 | 1 | 0 | 1 | 0 | +-------------+------+-------+-----+------+------+ | A | 11 | 1 | 1 | 0 | 0 | +-------------+------+-------+-----+------+------+ | B | 10 | 1 | 0 | 1 | 0 | +-------------+------+-------+-----+------+------+ | B | 10 | 1 | 1 | 0 | 0 | +-------------+------+-------+-----+------+------+ | B | 11 | 1 | 0 | 1 | 0 | +-------------+------+-------+-----+------+------+ | B | 11 | 1 | 1 | 0 | 0 | +-------------+------+-------+-----+------+------+ | C | 11 | 1 | 1 | 0 | 0 | +-------------+------+-------+-----+------+------+ | C | 11 | 1 | 1 | 0 | 0 | +-------------+------+-------+-----+------+------+
If you run the following SQL query, you will get the desired results without fear of repeating or repeating the loop later.
SELECT source_name, week, sum(picks), sum(won), sum(lost), sum(draw) FROM my_table GROUP BY source_name, week ORDER BY source_name
RESULT:
+-------------+------+------------+----------+-----------+-----------+ | source_name | week | sum(picks) | sum(won) | sum(lost) | sum(draw) | +-------------+------+------------+----------+-----------+-----------+ | A | 10 | 2 | 1 | 1 | 0 | +-------------+------+------------+----------+-----------+-----------+ | A | 11 | 5 | 3 | 2 | 0 | +-------------+------+------------+----------+-----------+-----------+ | B | 10 | 2 | 1 | 1 | 0 | +-------------+------+------------+----------+-----------+-----------+ | B | 11 | 2 | 1 | 1 | 0 | +-------------+------+------------+----------+-----------+-----------+ | C | 11 | 2 | 2 | 0 | 0 | +-------------+------+------------+----------+-----------+-----------+
See SQL FIDDLE to help you understand.
source share