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条)

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决