使用变量数组和foreach循环在PHP中显示XML节点时出现问题
我在显示存储在数组中的XML值时遇到一些问题。我成功地遍历了节点,但我正在尝试将数组全部回送到一起。 这是我的密码:使用变量数组和foreach循环在PHP中显示XML节点时出现问题,php,xml,arrays,for-loop,foreach,Php,Xml,Arrays,For Loop,Foreach,我在显示存储在数组中的XML值时遇到一些问题。我成功地遍历了节点,但我正在尝试将数组全部回送到一起。 这是我的密码: <?php $other_feed_url = "http://xml.pinnaclesports.com/pinnaclefeed.aspx?sporttype=Football&sportsubtype=NFL"; $xml2 = simplexml_load_file($other_feed_url); $AwayLine[] = (string
<?php
$other_feed_url = "http://xml.pinnaclesports.com/pinnaclefeed.aspx?sporttype=Football&sportsubtype=NFL";
$xml2 = simplexml_load_file($other_feed_url);
$AwayLine[] = (string)$xml2->spread_visiting;
$HomeLine[] = (string)$xml2->home_visiting;
$AwayMoneyLine[] = (string)$xml2->moneyline_visiting;
$HomeMoneyLine[] = (string)$xml2->moneyline_home;
$AwaySpreadAdjust[] = (string)$xml2->spread_adjust_visiting;
$HomeSpreadAdjust[] = (string)$xml2->spread_adjust_home;
$UnderAdjust[] = (string)$xml2->under_adjust;
$OverAdjust[] = (string)$xml2->over_adjust;
$TotalPoints[] = (string)$xml2->total_points;
for($i=0;$i<20; $i++) {
foreach ($xml2->events->event as $event) {
$Spread = $event->periods->period[0]->spread;
$MoneyLine = $event->periods->period[0]->moneyline;
$TotalPoints = $event->periods->period[0]->total;
$AwayLine[] = $Spread->spread_visiting;
$HomeLine[] = $Spread->spread_home;
$AwayMoneyLine[] = $MoneyLine->moneyline_visiting;
$HomeMoneyLine[] = $MoneyLine->moneyline_home;
$UnderAdjust[] = $TotalPoints->under_adjust;
$OverAdjust[] = $TotalPoints->over_adjust;
$TotalPoints[] = $TotalPoints->total_points;
}
echo '<br>';
echo $AwayLine[$i];
echo '<br>';
echo $HomeLine[$i];
echo '<br>';
echo $AwayMoneyLine[$i];
echo '<br>';
echo $HomeMoneyLine[$i];
echo '<br>';
echo $TotalPoints[$i];
}
?>
您正在设置$MoneyLine=$event->periods->period[0]->MoneyLine代码>,但这在正在加载的XML文档中不存在。然后尝试获取$AwayMoneyLine[]=$MoneyLine->MoneyLine\u代码>,没有结果值。有关正在解析的文档中的一个XML节点的示例,请参见下文:
<event>
<event_datetimeGMT>2012-11-02 00:25</event_datetimeGMT>
<gamenumber>272519903</gamenumber>
<sporttype>Football</sporttype>
<league>NFL</league>
<IsLive>No</IsLive>
<participants>
<participant>
<participant_name>Kansas City Chiefs</participant_name>
<contestantnum>301</contestantnum>
<rotnum>301</rotnum>
<visiting_home_draw>Visiting</visiting_home_draw>
</participant>
<participant>
<participant_name>San Diego Chargers</participant_name>
<contestantnum>302</contestantnum>
<rotnum>302</rotnum>
<visiting_home_draw>Home</visiting_home_draw>
</participant>
</participants>
<periods>
<period>
<period_number>0</period_number>
<period_description>Game</period_description>
<periodcutoff_datetimeGMT>2012-11-02 00:25</periodcutoff_datetimeGMT>
<period_status>I</period_status>
<period_update>open</period_update>
<spread_maximum>10000</spread_maximum>
<moneyline_maximum>5000</moneyline_maximum>
<total_maximum>3000</total_maximum>
<spread>
<spread_visiting>9.5</spread_visiting>
<spread_adjust_visiting>-130</spread_adjust_visiting>
<spread_home>-9.5</spread_home>
<spread_adjust_home>120</spread_adjust_home>
</spread>
<total>
<total_points>42.5</total_points>
<over_adjust>-102</over_adjust>
<under_adjust>-108</under_adjust>
</total>
</period>
</periods>
</event>
2012-11-02 00:25
272519903
足球
NFL
不
堪萨斯城酋长队
301
301
参观
圣地亚哥充电器
302
302
家
0
游戏
2012-11-02 00:25
我
打开
10000
5000
3000
9.5
-130
-9.5
120
42.5
-102
-108
您正在设置$MoneyLine=$event->periods->period[0]->MoneyLine代码>,但这在正在加载的XML文档中不存在。然后尝试获取$AwayMoneyLine[]=$MoneyLine->MoneyLine\u代码>,没有结果值。有关正在解析的文档中的一个XML节点的示例,请参见下文:
<event>
<event_datetimeGMT>2012-11-02 00:25</event_datetimeGMT>
<gamenumber>272519903</gamenumber>
<sporttype>Football</sporttype>
<league>NFL</league>
<IsLive>No</IsLive>
<participants>
<participant>
<participant_name>Kansas City Chiefs</participant_name>
<contestantnum>301</contestantnum>
<rotnum>301</rotnum>
<visiting_home_draw>Visiting</visiting_home_draw>
</participant>
<participant>
<participant_name>San Diego Chargers</participant_name>
<contestantnum>302</contestantnum>
<rotnum>302</rotnum>
<visiting_home_draw>Home</visiting_home_draw>
</participant>
</participants>
<periods>
<period>
<period_number>0</period_number>
<period_description>Game</period_description>
<periodcutoff_datetimeGMT>2012-11-02 00:25</periodcutoff_datetimeGMT>
<period_status>I</period_status>
<period_update>open</period_update>
<spread_maximum>10000</spread_maximum>
<moneyline_maximum>5000</moneyline_maximum>
<total_maximum>3000</total_maximum>
<spread>
<spread_visiting>9.5</spread_visiting>
<spread_adjust_visiting>-130</spread_adjust_visiting>
<spread_home>-9.5</spread_home>
<spread_adjust_home>120</spread_adjust_home>
</spread>
<total>
<total_points>42.5</total_points>
<over_adjust>-102</over_adjust>
<under_adjust>-108</under_adjust>
</total>
</period>
</periods>
</event>
2012-11-02 00:25
272519903
足球
NFL
不
堪萨斯城酋长队
301
301
参观
圣地亚哥充电器
302
302
家
0
游戏
2012-11-02 00:25
我
打开
10000
5000
3000
9.5
-130
-9.5
120
42.5
-102
-108
结果输出是什么?所需的输出是什么?目前只打印$AwayLine
和$HomeLine
数组。对于结果输出,我有一个目标,就是能够在最后显示所有变量。我确实使用$Spread、$MoneyLine和$TotalPoints嵌套了foreach循环,但除了$AwayLine和$HomeLine之外,我仍然无法打印任何内容。您是否尝试检查了$AwayMoneyLine
等包含的内容?也许它们只是空节点。请尝试使用var\u dump($AwayMoneyLine)
查看它们包含的内容,或检查生成的源代码以获取脚本。@Rob感谢您迄今为止的帮助。var_dump($AwayMoneyLine)的结果代码>确实导致了空值,这正是我所期望的。取出此代码后$Spread=$event->periods->period[0]->Spread代码>生成的代码为空,因此错误必须处理$AwayMoneyLine。有什么建议吗?谢谢。@BlaineHurtado$event->periods->period[0]->moneyline
不存在,请参见下面的答案。结果是什么?所需的输出是什么?目前只打印$AwayLine
和$HomeLine
数组。对于结果输出,我有一个目标,就是能够在最后显示所有变量。我确实使用$Spread、$MoneyLine和$TotalPoints嵌套了foreach循环,但除了$AwayLine和$HomeLine之外,我仍然无法打印任何内容。您是否尝试检查了$AwayMoneyLine
等包含的内容?也许它们只是空节点。请尝试使用var\u dump($AwayMoneyLine)
查看它们包含的内容,或检查生成的源代码以获取脚本。@Rob感谢您迄今为止的帮助。var_dump($AwayMoneyLine)的结果代码>确实导致了空值,这正是我所期望的。取出此代码后$Spread=$event->periods->period[0]->Spread代码>生成的代码为空,因此错误必须处理$AwayMoneyLine。有什么建议吗?谢谢。@BlaineHurtado$event->periods->period[0]->moneyline
不存在,请看下面我的答案。不久前就存在了。您是否认为最好输入一些代码,例如if(moneyline==NULL),将值赋给OFF?谢谢@BlaineHurtado不知道你说的是什么意思?是否要检查节点是否存在?如果您有任何建议,请选择“是”。否则我有一些想法。一个简单的if(isset($event->periods->period[0]->moneyline))
就可以了。不久前它还在那里。您是否认为最好输入一些代码,例如if(moneyline==NULL),将值赋给OFF?谢谢@BlaineHurtado不知道你说的是什么意思?是否要检查节点是否存在?如果您有任何建议,请选择“是”。否则我有一些想法。一个简单的if(isset($event->periods->period[0]->moneyline))
就可以了。