From: norbert_schuetz at bigfoot dot com
Operating system: Linux 2.6.18-5-686
PHP version: 5.2.6
PHP Bug Type: SimpleXML related
Bug description: SimpleXML inconsisten behaviour with multiple xml nodes
Description:
------------
PHP 5.2.0-8+etch11 (cli) (built: May 10 2008 10:46:24) from
php5-cli_5.2.0-8+etch10_i386.deb
Linux 2.6.18-5-686 #1 SMP Sun Aug 12 21:57:02 UTC 2007 i686 GNU/Linux
Simplexml support => enabled
Revision => $Revision: 1.151.2.22.2.15 $
Schema support => enabled
libxml2_2.6.27.dfsg-2_i386.deb
Problem description:
--------------------
For multiple identically named XML nodes the data types are not
consistent, results are possibly erratic or at least misleading:
* SimpleXML returns the first array element instead of returning the array
itself when accessing the node - although var_dump() reports the node to be
an array.
* gettype() reports "object" while var_dump() reports "array".
* processing the node with foreach reports the key to be the node name,
not the numeric array id reported by print_r().
Also, for single XML nodes SimpleXML
* gettype() reports "object" while var_dump() reports "array".
* The node content can be accessed as $xml->node as well as $xml->node[0].
If $xml->node is the node's content (a string, that is) $xml->node[0]
should return the first character of the string. At least the documentation
should note the ambitious behavior.
Reproduce code:
---------------
<?php
$xmlstr=<<<EOXML
<?xml version='1.0' standalone='yes'?>
<export>
<testdata>
<single>test</single>
<multiple>one</multiple>
<multiple>two</multiple>
<multiple>three</multiple>
</testdata>
</export>
EOXML;
$xml=new SimpleXMLElement($xmlstr);
print '1. var_dump($xml->testdata):' ."\n";
var_dump($xml->testdata);
print "\n";
print '2. get_type($xml->testdata->single): ';
print gettype($xml->testdata->single) ."\n";
print "\n";
print "3. print \$xml->testdata->single without and with [0]: \n";
print $xml->testdata->single ."\n";
print $xml->testdata->single[0] ."\n";
print "\n";
print '4. var_dump($xml->testdata->single): ';
var_dump($xml->testdata->single);
print "\n";
print '5. get_type($xml->testdata->multiple): ';
print gettype($xml->testdata->multiple) ."\n";
print "\n";
print '6. var_dump($xml->testdata->multiple): ';
var_dump($xml->testdata->multiple);
print "\n";
print '7. foreach($xml->testdata->multiple ...):' ."\n";
foreach ($xml->testdata->multiple as $key=>$item) {
print $key .' = ' . $item ."\n";
}
Expected result:
----------------
6. var_dump($xml->testdata->multiple): object(SimpleXMLElement)#4 (1)
array(3) {
[0]=>
string(3) "one"
[1]=>
string(3) "two"
[2]=>
string(5) "three"
}
7. foreach($xml->products->multiple ...):
0 = one
1 = two
2 = three
Actual result:
--------------
1. var_dump($xml->testdata):
object(SimpleXMLElement)#2 (2) {
["single"]=>
string(4) "test"
["multiple"]=>
array(3) {
[0]=>
string(3) "one"
[1]=>
string(3) "two"
[2]=>
string(5) "three"
}
}
2. get_type($xml->testdata->single): object
3. print $xml->testdata->single without and with [0]:
test
test
4. var_dump($xml->testdata->single): object(SimpleXMLElement)#3 (1) {
[0]=>
string(4) "test"
}
5. get_type($xml->testdata->multiple): object
6. var_dump($xml->testdata->multiple): object(SimpleXMLElement)#4 (1) {
[0]=>
string(3) "one"
}
7. foreach($xml->testdata->multiple ...):
multiple = one
multiple = two
multiple = three
--
Edit bug report at http://bugs.php.net/?id=45023&edit=1
--
Try a CVS snapshot (PHP 5.2):
http://bugs.php.net/fix.php?id=45023&r=trysnapshot52
Try a CVS snapshot (PHP 5.3):
http://bugs.php.net/fix.php?id=45023&r=trysnapshot53
Try a CVS snapshot (PHP 6.0):
http://bugs.php.net/fix.php?id=45023&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=45023&r=fixedcvs
Fixed in release:
http://bugs.php.net/fix.php?id=45023&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=45023&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=45023&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=45023&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=45023&r=support
Expected behavior: http://bugs.php.net/fix.php?id=45023&r=notwrong
Not enough info:
http://bugs.php.net/fix.php?id=45023&r=notenoughinfo
Submitted twice:
http://bugs.php.net/fix.php?id=45023&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=45023&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=45023&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=45023&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=45023&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=45023&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=45023&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=45023&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=45023&r=mysqlcfg