我目前正在使用以下代码
foreach ($xml->sport[0]->event[1] as $gameinfo):
加载Eng。超级联赛。有没有办法可以从id = 3的地方绘制数据,而不是使用[1]方法?
XML是:
<sports file_date="2012-11-16T14:30:27.853">
<sport name="Football" id="1">
<event name="World Cup" id="1"></event>
<event name="Eng. Premier League" id="3"></event>
<event name="French Ligue 1" id="4"></event>
<sport>
答案 0 :(得分:0)
这样做:
<?php
$string = '<sports file_date="2012-11-16T14:30:27.853">
<sport name="Football" id="1">
<event name="World Cup" id="1"></event>
<event name="Eng. Premier League" id="3"></event>
<event name="French Ligue 1" id="4"></event>
</sport>
</sports>';
$xml = simplexml_load_string( $string );
foreach( $xml->sport->children() as $gameinfo )
{
if( $gameinfo->attributes()->id == 3 )
{
// do your thing
}
}
?>