ID:               46517
 Comment by:       sites at hubmed dot org
 Reported By:      sites at hubmed dot org
 Status:           Open
 Bug Type:         SimpleXML related
 Operating System: Mac OS X
 PHP Version:      5.2CVS-2008-11-07 (CVS)
 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------

[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

Reply via email to