Array length in SimpleXML

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lhenkel@gmail.com

    #1

    Array length in SimpleXML

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

    I can do this:
    $xml = new SimpleXMLElemen t('<items>
    <item><name>Lee </name><count>1</count></item>
    <item><name>Bil l</name><count>5</count></item></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
    and
    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?

    Thanks,

    Lee

  • petersprc

    #2
    Re: Array length in SimpleXML

    Hi,

    Try maybe:

    echo "There are " . count($xml->xpath('/items/item')) . " top-level
    items.\n";

    lhenkel@gmail.c om wrote:
    This is idiotic, but I can't figure this out:
    >
    I can do this:
    $xml = new SimpleXMLElemen t('<items>
    <item><name>Lee </name><count>1</count></item>
    <item><name>Bil l</name><count>5</count></item></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
    and
    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?
    >
    Thanks,
    >
    Lee

    Comment

    Working...