1

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

3 Answers 3

1

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']
Sign up to request clarification or add additional context in comments.

Comments

0

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

$simpleXml = ...
$givenName = $simpleXml['givenName'];

1 Comment

$simpleXml = ... ? would this be written $simpleXml = simplexml_load_string($auth_info); $givenName = $simpleXml['givenName']; echo $givenName;
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.