我目前正在浏览足球game data,并尝试计算所有可能为seen here的信息。
但是我的代码似乎出错,take a look here,让我们以赫尔城市团队为例。
所以,下面是我的循环,如果我的班级的一个实例没有为该团队制作,则创建它,如果它被创建,它将使用现有的。
foreach ( $soccerseason->getAllFixtures()->fixtures as $v ) {
if ($v->status == "FINISHED") {
// Add data from home team
if (!array_key_exists($v->homeTeamName, $teamData)) {
$teamData[$v->homeTeamName] = new Calc(array($v->result->goalsHomeTeam, $v->result->goalsAwayTeam));} else {
$teamData[$v->homeTeamName]->addData(array($v->result->goalsHomeTeam, $v->result->goalsAwayTeam));
}
if (!array_key_exists($v->awayTeamName, $teamData)) {
$teamData[$v->awayTeamName] = new Calc(array($v->result->goalsHomeTeam, $v->result->goalsAwayTeam));} else {
$teamData[$v->awayTeamName]->addData(array($v->result->goalsHomeTeam, $v->result->goalsAwayTeam));
}
}
}
这是我的Calc
课程,这应该工作正常,我无法弄清楚这个问题。
class Calc {
public $config;
public $win = 0, $draw = 0, $loss = 0, $goalFor = 0, $goalConc = 0;
public function __construct($payload) {
// Load config file
$this->config = parse_ini_file('config.ini', true);
// Add wins, losses, draws, goals for and goal conceived
$this->addData($payload);
}
// Linked data updated, ammend values
public function calcPlays() {
return 0 + $this->win + $this->draw + $this->loss;
}
public function calcPoints() {
// Add 0 to ensure value is a int
return (0 + ($this->win * $this->config->winPoints) +
($this->draw * $this->config->drawPoints) +
($this->loss * $this->config->lossPoints));
}
public function calcGoalDifference() {
return ($this->goalFor + $this->goalConc);
}
public function addData($data) {
// Append goal data
$this->goalFor += $data[0]; $this->goalConc += $data[1];
if ($data[0] > $data[1]) { $this->win++; } elseif
($data[0] < $data[1]) { $this->loss++; } elseif
($data[0] == $data[1]) { $this->draw++; }
}
}
答案 0 :(得分:0)
事实证明,在计算客队是否赢了时我需要交换数据。
if (!array_key_exists($v->homeTeamName, $teamData)) {
$teamData[$v->homeTeamName] = new Calc(array($v->result->goalsHomeTeam, $v->result->goalsAwayTeam));
} else {
$teamData[$v->homeTeamName]->addData(array($v->result->goalsHomeTeam, $v->result->goalsAwayTeam));
}
if (!array_key_exists($v->awayTeamName, $teamData)) {
$teamData[$v->awayTeamName] = new Calc(array($v->result->goalsAwayTeam, $v->result->goalsHomeTeam));
} else {
$teamData[$v->awayTeamName]->addData(array($v->result->goalsAwayTeam, $v->result->goalsHomeTeam));
}