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

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.

SimpleXML provide __toString() method to return string value:

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