Getting Value from Object with Characters in Element Name

60 views Asked by At

I have an object being passed into a function which I have no control over and it is in the below format, with the root being 'entity'.

object(SimpleXMLElement)#6 (1) { ["@attributes"]=> array(2) { ["id"]=> string(2) "12" ["name"]=> string(17) "Test Object Value" } } 

Now I'm trying to pull out just the name by using both the below snippets but both output empty values.

entity[0]->name;

and

entity->{'@attributes'}->name;

Is there a special way to deal with characters in element names when the curly brackets format doesn't work?

2

There are 2 answers

0
VolenD On BEST ANSWER

You can get the name attribute as follows:

$name = $entity->attributes()->name;
echo $name;
0
Ikari On

You need to use attribute() function for getting the attributes in a simpleXML object. Your code should be something like:

$parsed = $simplexmlObject->entity->attribute()->desiredProperty;

Update: Got this technique from a question asked from me, How to parse value `@attribute` from a SimpleXMLObject in PHP