如何将name或name1变量设置为$ event的name属性的值?当我遍历代码名称时,等于一个没有值的simplexmlobject,并且name2为null。simplexml_load_string获取属性
$ name3 = $ event-> attributes() - > name;也不起作用。
我很困惑如何正确使用它。
$curl = curl_init();
curl_setopt_array($curl, Array(
CURLOPT_URL => 'http://odds.smarkets.com/oddsfeed.xml',
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_ENCODING => 'UTF-8'
));
$data = curl_exec($curl);
curl_close($curl);
$xml = simplexml_load_string($data);
$football = array();
foreach($xml->event as $event){
if($event->attributes()->type == "Football match"){
$att = $event->attributes();
$name = $att['name'];
$name2 = $att->attributes()->name;
$name3 = $event->attributes()->name;
$football[] = $event;
}
}
foreach($football as $game){
if($game->attributes()->name == "Sunderland vs. Manchester United"){
$a = $game;
}
}
它不工作?是的,它确实 – Ghost
我得到一个空的simplexmlobject。这可以工作$ game-> attributes() - > name ==“Sunderland vs. Manchester United”,但$ name = $ event-> attributes() - > name;才不是。 – user794846