I have 2 arrays $list and $list2:
$list:
Array
(
[0] => Array
(
[team] => 14
[team_points] => 3
[team_occurrences] => 2
)
[1] => Array
(
[team] => 1
[team_points] => 3
[team_occurrences] => 2
)
[2] => Array
(
[team] => 5
[team_points] => 1
[team_occurrences] => 1
)
[3] => Array
(
[team] => 13
[team_points] => 1
[team_occurrences] => 1
)
[4] => Array
(
[team] => 7
[team_points] => 0
[team_occurrences] => 1
)
[5] => Array
(
[team] => 2
[team_points] => 0
[team_occurrences] => 3
)
)
$list2:
Array
(
[0] => Array
(
[team] => 20
[team_points] => 7
[team_occurrences] => 3
)
[1] => Array
(
[team] => 10
[team_points] => 3
[team_occurrences] => 1
)
[2] => Array
(
[team] => 14
[team_points] => 3
[team_occurrences] => 1
)
[3] => Array
(
[team] => 13
[team_points] => 3
[team_occurrences] => 1
)
[4] => Array
(
[team] => 19
[team_points] => 3
[team_occurrences] => 1
)
[5] => Array
(
[team] => 17
[team_points] => 1
[team_occurrences] => 1
)
[6] => Array
(
[team] => 11
[team_points] => 0
[team_occurrences] => 1
)
[7] => Array
(
[team] => 15
[team_points] => 0
[team_occurrences] => 1
)
)
As you can see the columns are the same in both arrays (team, team_points, team_occurrences)
Now, I would like to merge these two arrays into array called $list_all
The problem with the merge is that standard merge I have tried
array_merge($list,$list2);
just add them together.
However, What I need is to count same teams e.g. [team] => 14 and [team] => 13 are in both arrays (in $list and $list2) , so, therefore I need to sum team_points column value from $list with and team_points column value from $list2 if the team is identical. The same for team_occurrences column.
So e.g.
New array will not look like this:
Array
(
[0] => Array // from $list
(
[team] => 14
[team_points] => 3
[team_occurrences] => 2
)
[1] => Array // from $list2
(
[team] => 14
[team_points] => 3
[team_occurrences] => 1
)
[3] => Array // from $list
(
[team] => 13
[team_points] => 1
[team_occurrences] => 1
)
[4] => Array // from $list2
(
[team] => 13
[team_points] => 3
[team_occurrences] => 1
)
But I need that it will look like this:
Array
(
[0] => Array
(
[team] => 14
[team_points] => 6
[team_occurrences] => 3
)
[1] => Array
(
[team] => 13
[team_points] => 4
[team_occurrences] => 2
)
After the merge I would like to sort the result array using usort() or maybe some better function by team_points DESC (from highest value to the lowest).
Thanks in advance for any advice.
Iterate over the first list, creating a new array whose keys are the team numbers. Then iterating over the second array, if the team already exists, add and otherwise, append.
Note: The above produces an output array keyed by team number. If you really just want ascending numeric keys, call
array_values()on it:Edit Fixed a few missing
]above…