-3

我有一个这样的数组:

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;

}
4

2 回答 2

0

解决方案:

function concatenateResults($standings) {
    $conc = array();
    foreach ($standings as $standing) {

        if (!isset($conc[$standing['serie_id']][$standing['team1_id']][$standing['team2_id']])) {           
            $conc[$standing['serie_id']][$standing['team1_id']][$standing['team2_id']]['team1_id']  = $standing['team1_id'];
            $conc[$standing['serie_id']][$standing['team1_id']][$standing['team2_id']]['team2_id']  = $standing['team2_id'];
            $conc[$standing['serie_id']][$standing['team1_id']][$standing['team2_id']]['wins']      = $standing['wins'];
            $conc[$standing['serie_id']][$standing['team1_id']][$standing['team2_id']]['draws']     = $standing['draws'];
            $conc[$standing['serie_id']][$standing['team1_id']][$standing['team2_id']]['losses']        = $standing['losses'];
            $conc[$standing['serie_id']][$standing['team1_id']][$standing['team2_id']]['serie_id']  = $standing['serie_id'];
        } else {
            $conc[$standing['serie_id']][$standing['team1_id']][$standing['team2_id']]['wins']      += $standing['wins'];
            $conc[$standing['serie_id']][$standing['team1_id']][$standing['team2_id']]['draws']         += $standing['draws'];
            $conc[$standing['serie_id']][$standing['team1_id']][$standing['team2_id']]['losses']        += $standing['losses'];         
        }
    }//foreach
        return $conc;   
}
于 2013-10-02T19:19:44.293 回答
-1

这样的事情应该可以解决问题:

$new_array = array();
foreach($main_array as $array) {
    $new_array[] = array("team1_id" => $array["team1_id"], "team2_id" => $array["team2_id"], "serie_id" => $array["serie_id"], "total" => ((int)$array["wins"] + (int)$array["draws"] + (int)$array["losses"]));
}
var_dump($new_array);

然后在您的 中new_array,您可以像这样访问总数:

$first_total = $new_array[0]["total"];

有关 foreach() 循环的更多文档

注意:我没有对此进行测试,只是脱离了先验知识

于 2013-10-02T18:26:09.543 回答