-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 が同じ要素について、wins、draws、losss の値が合計された新しい配列を取得したいと考えています。version_id は関係ありません。

最終的に、抜粋は 6 要素ではなく 3 要素にする必要があります。

これをすばやくエレガントに行うにはどうすればよいですか?

編集:

申し訳ありませんが、これが私が試したことです。しかし問題は、私は 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 に答える