如何將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