Re: [PHP-XML-DEV] SimpleXML behaves like chameleon

[email protected] (Christian Stocker) Mon, 31 Jan 2005 22:17:23 +0100
Newsgroups php.xml.dev
Message-ID <[email protected]>
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  |  [email protected]  |  gnupg-keyid 0x5CE1DECB