SimpleXML behaves like chameleon
[email protected] (Jirka Kosek) Mon, 31 Jan 2005 21:14:11 +0100
| Newsgroups | php.xml.dev |
|---|---|
| Message-ID | <[email protected]> |
Hi,
I found strange problem with SimpleXML. If there are multiple elements=20
with the same name on the same level (they are siblings) there are=20
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 =3D 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=20
now. Huh?!
print_r($xml->item->subitem);
?>
</pre>
TIA,
Jirka
--=20
------------------------------------------------------------------
Jirka Kosek e-mail: [email protected] http://www.kosek.cz
------------------------------------------------------------------
Profesion=E1ln=ED =B9kolen=ED a poradenstv=ED v oblasti technologi=ED =
XML.
Pod=EDvejte se na n=E1=B9 nov=EC spu=B9t=ECn=FD web http://DocBook.=
cz
Podrobn=FD p=F8ehled =B9kolen=ED http://xmlguru.cz/skoleni/
------------------------------------------------------------------