PHP数组计数时,ID和可变匹配
问题描述:
晚上好,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.
但不能弄明白。
任何帮助,将不胜感激。
亲切的问候, 帕特里克
答
继承人我会做什么
$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
创建关联数组的键是团队的ID,和值与'matchesWon','matchesLost',和关联数组'MatchesDraw'键。然后遍历返回的数据,为该团队ID递增适当的数组元素。 – Barmar
*期望您[尝试首先解决您自己的问题](http://meta.*.com/questions/261592),并且我们也[不回答作业问题](https://softwareengineering.meta。 stackexchange.com/questions/6166)。请更新您的问题,以显示您已经在[最小,完整和可验证的示例]中尝试过的内容(http://*.com/help/mcve)。有关更多信息,请参阅[如何提出良好问题](http://*.com/help/how-to-ask),并参加[网站之旅](http://*.com/tour ):) – Barmar