0

This is my xml object. I am unable to parse this in object array in my script.

SimpleXMLElement Object( [pagination] => SimpleXMLElement Object ( [@attributes] => Array ( [pageNumber] => 1 [pageSize] => 100 [totalAvailable] => 1 ) ) [users] => SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => xxxxxxxx [name] => xxxxx [siteRole] => xxxxx [lastLogin] => xxxxxx [externalAuthUserId] => ) ) ) ) 

Here is my PHP code:

foreach($xml2->users as $item) { $userName = $item->attributes()->name; } 

Am I doing something wrong?

2

1 Answer 1

1

I was able to solve the issue. Here is the modified code:

foreach($xml2->users[0] as $user) { $userName = $user['name']; } 

Thanks to ymas he pointed correctly.

Sign up to request clarification or add additional context in comments.

3 Comments

@StevenMoseley Both forms are technically correct, although I agree that the ['name'] format is neater. It's a borderline edit, though, because it completely changes the OP's answer.
@IMSoP good point, it was an extreme edit. However, the original answer will return a SimpleXMLElement Object, rather than a string value, no?
@StevenMoseley No, $foo->attributes()->bar and $foo['bar'] return exactly the same object. 3v4l.org/a3tQo If you want the string value, you have to cast to string, e.g. $userName = (string)$user['name'];

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.