ID: 45023 Updated by: [EMAIL PROTECTED] Reported By: norbert_schuetz at bigfoot dot com -Status: Open +Status: Bogus Bug Type: SimpleXML related Operating System: Linux 2.6.18-5-686 -PHP Version: 5.2.6 +PHP Version: 5.2.0 New Comment:
Thank you for taking the time to report a problem with PHP. Unfortunately you are not using a current version of PHP -- the problem might already be fixed. Please download a new PHP version from http://www.php.net/downloads.php If you are able to reproduce the bug with one of the latest versions of PHP, please change the PHP version on this bug report to the version you tested and change the status back to "Open". Again, thank you for your continued support of PHP. Previous Comments: ------------------------------------------------------------------------ [2008-05-16 17:56:41] norbert_schuetz at bigfoot dot com 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 this bug report at http://bugs.php.net/?id=45023&edit=1