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.
$combined = array();
// First loop creates array keys in $combined array
// based on team numbers
foreach($list as $initial) {
$combined[$initial['team']] = $initial;
}
// Second loop looks at $list2 and either adds to values
// if the team key already exists, or just adds the
// whole subarray on if it doesn't.
foreach($list2 as $secondary) {
// If it was in the first, append to it.
if (isset($combined[$secondary['team']])) {
$combined[$secondary['team']]['team_points'] += $secondary['team_points'];
$combined[$secondary['team']]['team_occurrences'] += $secondary['team_occurrences'];
}
// Otherwise, just add it to the array
else {
$combined[$secondary['team']] = $secondary;
}
}
// Then sort the combined array with usort() into descending order by points
// Anonymous callback function is PHP 5.3+. Needs to be a string value of a named function
// for PHP < 5.3
usort($combined, function($a, $b) {
if ($a['team_points'] === $b['team_points']) {
return 0;
}
else {
return $a['team_points'] < $b['team_points'] ? 1 : -1;
}
});
Note: The above produces an output array keyed by team number. If you really just want ascending numeric keys, call array_values()
on it:
// Strip off the team number keys and just use ascending numbers
$combined = array_values($combined);
Edit Fixed a few missing ]
above...
Easiest is sorting both arrays on team number (using usort
). Walk through both arrays at the same time and pick the lowest teamnumber. If list1 is lowest, add that team to the result and increase the list1 index. If list2 is lowest, add that team to the result and increase list2 index. If both teams are the same, combine them by adding the points and occurrences. Repeat until your at the end of one list and add al remaining elements from the other list.
You can do it the following way:
foreach($list2 as $ky => $val) {
$list2['t'.$val['team']] = $val;
}
$tot = count($list1);
$list3 = array();
for ($i=0;$i<$tot;$i++) {
$team = $list1[$i]['Team'];
$list3[]['Team'] = $team;
$points = isset($list2['t'+$team]) ? $list2['t'+$team]['team_points'] : 0;
$list3[]['team_points'] = $list1[i]['team_points'] + $points;
$ocu = isset($list2['t'+$team]) ? $list2['t'+$team]['team_ocurrences'] : 0;
$list3[]['team_ocurrences'] = $list1[$i]['team_ocurrences'] + $ocu;
}
function cmp($a, $b) {
if ($a['team_points'] == $b['team_points']) {
return 0;
}
return ($a['team_points'] > $b['team_points']) ? -1 : 1;
}
usort($list3, "cmp");