user912
user912

Reputation: 171

How to get an xml element by index

I have an xml file that contains a list of elements. I want to retrieve a specific element by index. I looked around for a while and didn't find anything that worked for me.

I would have thought this would work, but it doesn't:

echo($xml->children()[0]);

I'm not very experienced in php, coming from a C# background, so any help is appreciated.

Upvotes: 0

Views: 1715

Answers (1)

Phil
Phil

Reputation: 164811

Array dereferencing was added in PHP 5.4 so your code will (probably) work with that version.

If you're stuck with an older version, simply try this

$children = $xml->children();
echo $children[0];

Upvotes: 2

Related Questions