dqwh0109 2016-02-12 00:17
浏览 133
已采纳

simplexml_load_string获取属性

How do I set the name or name1 variable to value of the $event's name attribute? When I step through the code name equals a simplexmlobject with no value and name2 is null.

$name3 = $event->attributes()->name; does not work either.

I am confused on how to use this properly.

$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;
   }
 }
  • 写回答

2条回答 默认 最新

  • doujiexi1824 2016-02-12 00:57
    关注

    Some of the script works, it goes get the game Sunderland. The part that doesn't make sense are the following:

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

    You don't need to use ->attributes over $att it already has the attributes.

    If you want to search for that Sunderland game, just remove some it the unneeded parts:

    $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
    

    If you really want to assign that particular name into a variable, just typecast the attribute into (string), ala:

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

    Edit: Just go to the appropriate level using foreach if you have to. Just add an is_array to do some checking. Some levels have a flat one on prices. Some have multiple prices on some offers. Do this checking accordingly. To get you going, here's an example:

    $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
    
                }
    
            }
    
        }
    }
    

    The concept is still the same, use typecasting on the attributes when needed. This should get you going.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!