我有一个我正在阅读的XML文件,它提供了匹配信息。
这是文件内容(保存浪费空间的部分内容)
<match>
<competition>Barclays Premier League</competition>
<date>Tuesday 2nd February 2016</date>
<time>19:45:00</time>
<status>Full Time</status>
<team>
<side>home</side>
<no>42</no>
<name>West Ham United</name>
<halfTimeScore>0</halfTimeScore>
<score>2</score>
<goal>Michail Antonio (58')</goal>
<goal>Cheikhou Kouyate (85')</goal>
<player>
<shirtNo>5</shirtNo>
<name>James Tomkins</name>
</player>
<player>
<shirtNo>19</shirtNo>
<name>James Collins</name>
<cautioned>
<minute>67</minute>
</cautioned>
</player>
</team>
<team>
<side>away</side>
<no>2</no>
<name>Aston Villa</name>
<halfTimeScore>0</halfTimeScore>
<score>0</score>
<player>
<shirtNo>18</shirtNo>
<name>Kieran Richardson</name>
</player>
<player>
<shirtNo>38</shirtNo>
<name>Jordan Lyden</name>
</player>
</team>
<attendance>34914</attendance>
<venue>Upton Park</venue>
我在尝试获取“目标”信息时遇到了问题。
到目前为止,我的代码是:
$location = 'http://www.footballwebpages.co.uk/match.xml?match=15321';
$xml = @file_get_contents($location);
$httpcode = $http_response_header[0];
$httpcode = substr($httpcode, 9, 3);
if ($httpcode==429) {
break;
} else if ($httpcode>=200 && $httpcode<300) {
$xmltext = simplexml_load_string($xml);
$comp = $xmltext->competition;
$date = $xmltext->date;
$status = $xmltext->status;
$team_no = 0;
foreach($xmltext->team as $update) {
/** Grab Info */
$team_side[$team_no] = $update->side;
$team_id[$team_no] = $update->no;
$team_name[$team_no] = $update->name;
$team_ht_score[$team_no] = $update->halfTimeScore;
$team_score[$team_no] = $update->score;
if (isset($update->goal)) {
$goalno = 0;
foreach ($update->goal as $goal) {
$goal[$team_no][$goalno] = $goal;
$goalno ++;
}
}
/* Grab Player Info */
$playerinfo[$team_no] = "";
foreach($update->player as $player) {
$playerno = $player->shirtNo;
$playername = $player->name;
/* caution */
if (isset($player->cautioned)) {
$playercaution = $player->cautioned->minute;
} else {
$playercaution = "";
}
}
$team_no ++;
}
}
与我一起捣乱的部分是:
if (isset($update->goal)) {
$goalno = 0;
foreach ($update->goal as $goal) {
$goal[$team_no][$goalno] = $goal;
$goalno ++;
}
}
我错过了什么?
答案 0 :(得分:0)
在您的代码中:
if (isset($update->goal)) {
$goalno = 0;
foreach ($update->goal as $goal) {
$goal[$team_no][$goalno] = $goal;
$goalno ++;
}
}
您通过foreach
SimpleXMLElement对象执行$update->goal
循环$goal
,然后将此值分配给名为$goal
的数组,但$goal
是一个SimpleXMLElement对象,而不是数组。
只需更改$goal
数组的名称。