2016-02-12 206 views
1

如何將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; 
    } 
} 
+0

它不工作?是的,它確實 – Ghost

+0

我得到一個空的simplexmlobject。這可以工作$ game-> attributes() - > name ==「Sunderland vs. Manchester United」,但$ name = $ event-> attributes() - > name;纔不是。 – user794846

回答

0

一些腳本的作品,它去獲得遊戲Sunderland。這沒有任何意義的部分如下:

$att = $event->attributes(); // gets all the attributes 
// using `$att` then invokes the attributes method 
$name2 = $att->attributes()->name; 

你並不需要使用->attributes超過$att它已經具有的屬性。

如果你要搜索的是Sunderland遊戲,只是刪除了一些它不需要的部分:

$xml = simplexml_load_string($data); 
$football = array(); 
foreach($xml->event as $event){ 
    if($event->attributes()->type == "Football match"){ 
     $football[] = $event; // push foot ball matches 
    } 
} 

foreach($football as $game){ 
    // search for sunderland game 
    if($game->attributes()->name == "Sunderland vs. Manchester United"){ 
     $a = $game; 
    } 
} 

print_r($a); // checker 

如果你真的希望將特定名稱分配到一個變量,只是強制轉換屬性爲(string),ALA :

$name = (string) $event->attributes()->name; 

編輯: 只要去使用foreach如果你有合適的水平。只需添加一個is_array來做一些檢查。價格水平有限。有些優惠有些會有多種價格。做相應的檢查。讓你去,這裏有一個例子:

$match = 'Sunderland vs. Manchester United'; 
foreach($football as $game){ 
    if($game->attributes()->name == $match){ 
     // if that game is found 
     foreach($game->market as $market) { 

      foreach($market->contract as $contract) { 

       // offers 
       if(!empty($contract->offers)) { 
        foreach($contract->offers->price as $price) { 
         // single level price 
         if(!is_array($price)) { 
          $decimal = (string) $price->attributes()->decimal; 
          echo $decimal; 
         } 
         // multi leveled price 
         else { 
          foreach($price as $p) { 
           $decimal = (string) $p->attributes()->decimal; 
           echo $decimal; 
          } 
         } 


        } 
       } 
       // end offers 

      } 

     } 

    } 
} 

的概念仍然是相同的,在需要的時候使用強制類型轉換上的屬性。這應該讓你去。

+0

注意:dammit,無法找到一個體面的在線演示網站,如果你想測試這個答案,代碼是在[這裏](http://pastebin.com/QDa8UUAg),然後複製後,粘貼它到此代碼[演示網站](http://stargento.com)來檢查 – Ghost

+0

這是我錯過的類型演員。我仍然覺得這很難獲得我後面的數據。基本上我想把所有的足球比賽都變成一系列標準的php對象,然後我可以循環並獲得所有賠率價格。 – user794846

+0

@ user794846你應該在這個問題中包括這個問題,現在提供的答案是不完整的,哪些是那裏的單價?在xml中有很多價格數據,請注意這部分的具體細節 – Ghost

0

的SimpleXML提供__toString()方法返回的字符串值:

$name = $event->attributes()->name->__toString();