2016-08-02 81 views
1

這裏是我的多維數組:顯示獨特的結果,並計算重複結果多維數組

Array 
(
    [0] => Array 
     (
      [H2H_Id] => T32 
      [Team1_Id] => T4 
      [Team1] => Juan Arraya - Max LePivert 
      [Team2_Id] => T205 
      [Team2] => Marco Grangeiro - Jeff Morneau 
      [Winners_Id] => T4 
      [MatchUps_Id] => M32 
     ) 

    [1] => Array 
     (
      [H2H_Id] => T39 
      [Team1_Id] => T205 
      [Team1] => Marco Grangeiro - Jeff Morneau 
      [Team2_Id] => T4 
      [Team2] => Juan Arraya - Max LePivert 
      [Winners_Id] => T205 
      [MatchUps_Id] => M32 
     ) 

    [2] => Array 
     (
      [H2H_Id] => T9 
      [Team1_Id] => T3 
      [Team1] => Marco Grangeiro - George Wilkinson 
      [Team2_Id] => T4 
      [Team2] => Juan Arraya - Max LePivert 
      [Winners_Id] => T4 
      [MatchUps_Id] => M9 
     ) 

) 

我所要的輸出是:

Juan Arraya - Max LePivert 1 vs. 1 Marco Grangeiro - Jeff Morneau 
Juan Arraya - Max LePivert 1 vs. 0 Marco Grangeiro - George Wilkinson 

所以基本上,我想顯示通過使用MatchUps_Id分別進行不同的匹配,並打印隊名Team1Team2。根據Winners_Id,我想添加一個計數器來跟蹤每場比賽每場比賽的勝率。

請記住,這些結果來自用戶輸入,這會使得多維數組在每次請求來自field1field2傳遞的用戶的輸入時都會有所不同。

這是我到目前爲止的代碼:

$query2 = "SELECT HeadToHead.H2HNo H2H_Id, H2HTeam1Id Team1_Id, H2HTeam1 Team1, H2HTeam2Id Team2_Id, H2HTeam2 Team2, WinnersId Winners_Id, MatchUps.MatchUpsNo MatchUps_Id 
    FROM HeadToHead 
    JOIN MatchUps ON HeadToHead.MatchUpsNo=MatchUps.MatchUpsNo 
    WHERE ((H2HTeam1 LIKE '%$field1%' OR H2HTeam2 LIKE '%$field1%') AND (H2HTeam1 LIKE '%$field2%' OR H2HTeam2 LIKE '%$field2%'))"; 
$result2 = mysql_query($query2); 
$num2 = mysql_num_rows($result2); 

$arr2 = array(); 
if($num2 > 0) { 
    while($row2 = mysql_fetch_assoc($result2)) { 
     $arr2[] = $row2; 
    } 
} 

我從上面的代碼所期望的結果。

我想我已經在這件事情上做了大量的研究,但我試圖完成的事情似乎與那裏的不同。

任何幫助,非常感謝。

回答

1

您可以組對決一起通過'MatchUps_Id'這樣的:

foreach ($arr2 as $match) { 

    // create some short ids to make the next part more readable  
    $id = $match['MatchUps_Id']; 
    $t1 = $match['Team1_Id']; 
    $t2= $match['Team2_Id']; 

    if (!isset($matchups[$id][$t1]['name'])) { 
     // initialize if this matchup has not yet been created 
     $matchups[$id][$t1]['name'] = $match['Team1']; 
     $matchups[$id][$t2]['name'] = $match['Team2']; 
     $matchups[$id][$t1]['wins'] = (int) ($match['Winners_Id'] == $match['Team1_Id']); 
     $matchups[$id][$t2]['wins'] = (int) ($match['Winners_Id'] == $match['Team2_Id']); 
    } else { 
     // increment wins if the matchup already exists 
     $matchups[$id][$t1]['wins'] += $match['Winners_Id'] == $match['Team1_Id']; 
     $matchups[$id][$t2]['wins'] += $match['Winners_Id'] == $match['Team2_Id'];  
    } 
} 

使用$t1$t2爲二級鍵可以讓你增加對各自球隊的勝利數,而無需知道哪個是哪個。表達式$match['Winners_Id'] == $match['Team1_Id']返回一個布爾值,當與+=一起使用時,它將隱式轉換爲整數01,但在初始化時必須使用(int)明確地轉換。

已分組陣列,並且計數勝後,可以輸出的結果是這樣的:

foreach ($matchups as $matchup) { 
    list($a, $b) = array_values($matchup); 
    echo "$a[name] $a[wins] vs. $b[wins] $b[name]<br>"; 
} 
+0

這是完美的,不要驚慌!非常感謝你!我似乎無法理解如何操作多維數組。如果您有任何資源可以閱讀,請傳遞給您。再次感謝 –

+0

不客氣。我不知道任何特定的閱讀資源,但我可以嘗試提供一些建議。如果我被困在這樣的問題上,手動創建一個我試圖實現的結果的例子通常會很有幫助,並且從它的起點開始工作。對我來說,讓我可以看到更容易的東西,而不是試圖在我的腦海中做所有事情。 –

+0

這很有道理。再次,我感謝幫助 –