#46517 [Opn->Csd]: SimpleXML inconsistent behaviour with multiple XML nodes
| From: | vrana@php.net | Date: | Fri, 20 Nov 2009 12:21:57 +0000 |
| Subject: | #46517 [Opn->Csd]: SimpleXML inconsistent behaviour with multiple XML nodes | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-3285@lists.php.net to get a copy of this message | ||
ID: 46517
Updated by: vrana@php.net
Reported By: sites at hubmed dot org
-Status: Open
+Status: Closed
Bug Type: Documentation problem
Operating System: *
PHP Version: 5CVS, 6CVS (2008-11-11)
New Comment:
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation
better.
Previous Comments:
------------------------------------------------------------------------
[2009-11-20 12:21:56] svn@php.net
Automatic comment from SVN on behalf of vrana
Revision: http://svn.php.net/viewvc/?view=revision&revision=291073
Log: Explain properties type (bug #46517)
------------------------------------------------------------------------
[2008-11-11 12:56:16] sites at hubmed dot org
Thanks. I also found that count($xml->item) works as expected, which is
useful.
------------------------------------------------------------------------
[2008-11-11 11:15:51] jani@php.net
This explains what happens quite good:
http://vega.rd.no/article/simplexml-not-that-simple
Documentation is a bit vague about this though. Reclassified.
------------------------------------------------------------------------
[2008-11-07 16:03:21] sites at hubmed dot org
Actually, it seems that using foreach($xml->item) works the same
whether there's one item or multiple, so it's possible to work around
this using the following:
==========
$items = array();
foreach ($xml->item as $item)
$items[] = $item;
==========
That seems to always produce an array of the appropriate objects.
------------------------------------------------------------------------
[2008-11-07 15:30:19] sites at hubmed dot org
Description:
------------
This is a duplicate of bug 45023, which is marked as bogus but needs
reopening.
I'm seeing the same problem with the latest CVS version:
PHP 5.2.7RC2-dev (cli) (built: Nov 7 2008 15:02:08)
Mac OS 10.5.5
libxml2 2.6.30
At the moment, it's not possible to check whether a SimpleXML element
is an array or an object and iterate appropriately (as foreach() will
iterate over both an array and an object).
Reproduce code:
---------------
$xml =
simplexml_load_string('<root><item>a</item><item>b</item></root>');
print_r($xml);
foreach ($xml->item as $item)
print "\n" . 'Item: ' . (string) $item;
print "\n";
print'Type: ' . gettype($xml->item);
print "\n";
print is_array($xml->item) ? 'Is an array' : 'Not an array';
print "\n";
print_r($xml->item);
print_r($xml->item[1]);
Expected result:
----------------
gettype($xml->item) should return 'array', and is_array($xml->item)
should return true.
$xml->item should retrieve the whole array, not just the first item.
Actual result:
--------------
SimpleXMLElement Object
(
[item] => Array
(
[0] => a
[1] => b
)
)
Item: a
Item: b
Type: object
Not an array
SimpleXMLElement Object
(
[0] => a
)
SimpleXMLElement Object
(
[0] => b
)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=46517&edit=1