tags:

views:

148

answers:

3

how do i display individual values from the following code?

SimpleXMLElement Object ( 
      [@attributes] => Array 
      ( 
                        [stat] => ok 
                        ) 
                        [profile] => SimpleXMLElement Object 
                        ( 
                        [address] => SimpleXMLElement Object 
                        ( 
                        [country] => United Kingdom 
                        ) 
                        [displayName] => gareth 
                        [name] => SimpleXMLElement Object 
                        ( 
                        [givenName] => Gareth 
                        [familyName] => Davies 
                        [formatted] => Gareth Davies 
                        ) 
                        [preferredUsername] => gareth 
                        [providerName] => Google 
                        [verifiedEmail] => [email protected] 
                        )
                        )
A: 

Attributes in SimpleXML elements can be displayed by simply using the array accessor.

$simpleXml = ...
$givenName = $simpleXml['givenName'];
cletus
$simpleXml = ... ?would this be written$simpleXml = simplexml_load_string($auth_info); $givenName = $simpleXml['givenName'];echo $givenName;
gareth
+1  A: 

The SimpleXML section of the PHP Manual does a good job showing how to access data in the object.

Taken (some editing) from the manual, this shows the basics:

Accessing a node:

$xml->movie //first 'movie' node

Accessing a specific node

$xml->movie[0] //first 'movie' node

Accessing a secondlevel node:

$xml->movie[0]->rating

Accessing a node's attribute:

$xml->movie[0]->rating['type']
Tim Lytle
A: 

Array accessors only work for the @attributes sub-keys.

You can either use the arrow notation to access the properties:

$simpleXml->name->formatted;

Or cast the SimpleXmlElements individually to use array accessors:

$sxa = (array)$simpleXml->profile;
echo $sxa['displayName'];

If you go the casting route, you have cast at every level containing additional elements.

scribble