Q:

PHP Array count when ID and variable is a match

I got a script that's calculating how many matches are played with all the additional information like, point, goals scored, goals scored against you..
But I also want to know how many matches the teams have won / draw / lost etc.
I just cant seem to get it.
in the database table I got the following field called  
(homePoints, awayPoints, homeResult, awayResult)
(3,          0,          win,        lose)

This is part of the array I'm getting returned:
  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)

Now as you see the TeamID is matching but the MatchResults aren't, I would like to either make a subarray something like
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.

A:

Heres what I would do
$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;
    }
}

