如何添加数组的值?

时间:2013-10-02 18:15:02

标签: php

我有一个这样的数组:

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个元素。我想获得一个新数组,其中wins1_id,team2_id和serie_id相同的元素的wins,draw,loss值相加。 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;

}

2 个答案:

答案 0 :(得分: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;   
}

答案 1 :(得分:-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"];

More documentation on the foreach() loop

注意:我没有对此进行测试,只是取消先前的知识