2017-11-10 61 views
0

晚上好,PHP數組計數時,ID和可變匹配

我打破我的頭以下... 我得到多數民衆贊成計算有多少場比賽有像所有其他信息都起到了腳本,點,進球,進球對你..

但我也想知道有多少匹配的球隊都贏得/平局的目標/丟失等

我只是不能似乎得到它。 數據庫中的表,我得到了以下字段名爲

(homePoints, awayPoints, homeResult, awayResult) 
(3,   0,   win,  lose) 

這是我得到返回的數組的一部分:

2 => 
    array (size=7) 
     'TeamID' => string '1' (length=1) 
     'Matches' => string '1' (length=1) 
     'GoalsVoor' => string '2' (length=1) 
     'GoalsTegen' => string '0' (length=1) 
     'DoelSaldo' => int 2 
     'Punten' => string '3' (length=1) 
     'MatchResult' => string 'win' (length=3) 
    3 => 
    array (size=7) 
     'TeamID' => string '1' (length=1) 
     'Matches' => string '2' (length=1) 
     'GoalsVoor' => string '2' (length=1) 
     'GoalsTegen' => string '7' (length=1) 
     'DoelSaldo' => int -5 
     'Punten' => string '0' (length=1) 
     'MatchResult' => string 'lose' (length=4) 

現在,當你看到TeamID是匹配,但MatchResults麥凱納噸,我想或者使一個子陣列像

TeamID => 1 
MatchesWon => 1 and adding if the win more. 
MatchesLost => 1 and adding if they lose more. 
MatchesDraw => 0 and adding if they draw more. 

但不能弄明白。

任何幫助,將不勝感激。

親切的問候, 帕特里克

+0

創建關聯數組的鍵是團隊的ID,和值與'matchesWon','matchesLost',和關聯數組'MatchesDraw'鍵。然後遍歷返回的數據,爲該團隊ID遞增適當的數組元素。 – Barmar

+1

StackOverflow期望您[嘗試首先解決您自己的問題](http://meta.stackoverflow.com/questions/261592),並且我們也[不回答作業問題](https://softwareengineering.meta。 stackexchange.com/questions/6166)。請更新您的問題,以顯示您已經在[最小,完整和可驗證的示例]中嘗試過的內容(http://stackoverflow.com/help/mcve)。有關更多信息,請參閱[如何提出良好問題](http://stackoverflow.com/help/how-to-ask),並參加[網站之旅](http://stackoverflow.com/tour ):) – Barmar

回答

0

繼承人我會做什麼

$teams = array(); // Empty array for your teams 
$results = array('wins' => 0, 'draws' => 0, 'losses' = 0); // Starting array for each team 
foreach ($array as $a) { // Loop over your database array 
    $id = $a['TeamID']; // Use TeamID as your array keys 
    if (!isset($teams[$id])) { // Check if team is already in your teams array 
     $teams[$id] = $results; // If not then insert them with the starting array 
    } 
    if ($a['Punten'] == 0) {   // Now loop over the points column and increment results 
     $teams[$id]['losses'] += 1; 
    } 
    if ($a['Punten'] == 1) { 
     $teams[$id]['draws'] += 1; 
    } 
    if ($a['Punten'] == 3) { 
     $teams[$id]['wins'] += 1; 
    } 
} 
+0

謝謝,你幫忙。不能相信我是如此接近,但我從來沒有建立$結果數組,我試圖增加它在源數組:)謝謝一百萬 – PatrickStel