我有一个这样的数组:
array(450)
{
[0] array(7)
{
["team1_id"] int(1)
["team2_id"] int(2)
["wins"] int(2)
["draws"] int(0)
["losses"] int(0)
["version_id"] int(24)
["serie_id"] int(2)
}
[1] array(7)
{
["team1_id"] int(1)
["team2_id"] int(3)
["wins"] int(1)
["draws"] int(0)
["losses"] int(1)
["version_id"] int(24)
["serie_id"] int(2)
}
[2] array(7)
{
["team1_id"] int(1)
["team2_id"] int(4)
["wins"] int(0)
["draws"] int(2)
["losses"] int(0)
["version_id"] int(24)
["serie_id"] int(2)
}
[3] array(7)
{
["team1_id"] int(1)
["team2_id"] int(2)
["wins"] int(1)
["draws"] int(0)
["losses"] int(1)
["version_id"] int(23)
["serie_id"] int(2)
}
[4] array(7)
{
["team1_id"] int(1)
["team2_id"] int(3)
["wins"] int(1)
["draws"] int(0)
["losses"] int(1)
["version_id"] int(23)
["serie_id"] int(2)
}
[5] array(7)
{
["team1_id"] int(1)
["team2_id"] int(4)
["wins"] int(0)
["draws"] int(0)
["losses"] int(2)
["version_id"] int(23)
["serie_id"] int(2)
}
代码只是一个摘录 - 该数组有 450 个元素。我想获得一个新数组,其中将 team1_id、team2_id 和 serie_id 相同的元素的获胜、平局、损失的值相加。version_id 无关紧要。
最后,摘录应该是 3 个元素而不是 6 个。
我怎样才能快速而优雅地做到这一点?
编辑:
对不起,这是我尝试过的。但问题是我只考虑了team1_id,但我需要team1_id、team2_id和serie_id的组合,然后应该加起来。那么我应该需要做什么:if ($bla[$standing['team1_id']][$standing['team2_id']][$standing['serie_id']])
?
foreach ($standings as $standing) {
if (!isset($bla[$standing['team1_id']])) {
$bla[$standing['team1_id']]['team1_id'] = $standing['team1_id'];
$bla[$standing['team1_id']]['team2_id'] = $standing['team2_id'];
$bla[$standing['team1_id']]['wins'] = $standing['wins'];
$bla[$standing['team1_id']]['draws'] = $standing['draws'];
$bla[$standing['team1_id']]['losses'] = $standing['losses'];
$bla[$standing['team1_id']]['serie_id'] = $standing['serie_id'];
} else {
$bla[$standing['team1_id']]['wins'] += $standing['wins'];
$bla[$standing['team1_id']]['draws'] += $standing['draws'];
$bla[$standing['team1_id']]['losses'] += $standing['losses'];
}
return $bla;
}