2016-02-12 201 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();