php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #46517 SimpleXML inconsistent behaviour with multiple XML nodes
Submitted: 2008-11-07 15:30 UTC Modified: 2009-11-20 12:21 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: sites at hubmed dot org Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5CVS, 6CVS (2008-11-11) OS: *
Private report: No CVE-ID: None
 [2008-11-07 15:30 UTC] 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
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-07 16:03 UTC] 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-11 11:15 UTC] 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-11 12:56 UTC] sites at hubmed dot org
Thanks. I also found that count($xml->item) works as expected, which is useful.
 [2009-11-20 12:21 UTC] 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)
 [2009-11-20 12:21 UTC] vrana@php.net
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.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Sep 03 04:00:02 2025 UTC