SimpleXML behaves like chameleon
| From: | Jirka Kosek | Date: | Mon, 31 Jan 2005 20:14:11 +0000 |
| Subject: | SimpleXML behaves like chameleon | ||
| Groups: | php.xml.dev | ||
| Request: | Send a blank email to php-xml-dev+get-525@lists.php.net to get a copy of this message | ||
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:
<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
--
------------------------------------------------------------------
Jirka Kosek e-mail: jirka@kosek.cz http://www.kosek.cz
------------------------------------------------------------------
Profesionální školení a poradenství v oblasti technologií XML.
Podívejte se na náš nově spuštěný web http://DocBook.cz
Podrobný přehled školení http://xmlguru.cz/skoleni/
------------------------------------------------------------------