Array length in SimpleXML

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
This is idiotic, but I can't figure this out:

I can do this:
$xml = new SimpleXMLElement('<items>
foreach ($xml->item as $item) {
   echo $item->name, '<br />';

But I can't figure how to tell I have 2 items.  The obvious:

print count($xml->item);  // same for $xml->items
print $xml->item->length;  // same for $xml->items->length

They don't work the way I expect..

This is my first foray into XML, so I'm sure I'm doing something
stupid.. would anyone mind pointing it out?  



Re: Array length in SimpleXML


Try maybe:

    echo "There are " . count($xml->xpath('/items/item')) . " top-level
items.\n"; wrote:
Quoted text here. Click to load it

Site Timeline