Re: SimpleXML behaves like chameleon

From: Date: Mon, 31 Jan 2005 21:17:23 +0000
Subject: Re: SimpleXML behaves like chameleon
References: 1  Groups: php.xml.dev 
Request: Send a blank email to php-xml-dev+get-526@lists.php.net to get a copy of this message
Hi On 31.1.2005 21:14 Uhr, Jirka Kosek wrote:
Hi, I found strange problem with SimpleXML. If there are multiple elements with the same name on the same level (they are siblings) there are sometimes returned as an array and sometimes as an object. Is this a bug or am I missing something? See code that demonstrates problem:
Simple answer, the usual array functions don't work on simplexml objects. Use the iterator interface. chregu
<pre> <?php $xml = simplexml_load_string(" <root> <item>
    <subitem>A</subitem>
    <subitem>B</subitem>
</item> </root> "); // Inspect $xml->item. It seems that $xml->item->subitem is array print_r($xml->item); // OK. Count number of subitems. It should be 2 echo count($xml->item->subitem); echo "\n"; // But we get 1. Why? // It seems that $xml->item->subitem is not array anymore, it is object now. Huh?! print_r($xml->item->subitem); ?> </pre> TIA,
                Jirka
-- christian stocker | Bitflux GmbH | schoeneggstrasse 5 | ch-8004 zurich phone +41 1 240 56 70 | mobile +41 76 561 88 60 | fax +41 1 240 56 71 http://www.bitflux.ch | chregu@bitflux.ch | gnupg-keyid 0x5CE1DECB

« previous php.xml.dev (#526) next »