|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2008-09-11 14:25 UTC] rrichards@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 08:00:01 2025 UTC |
Description: ------------ When I run the following script I get strange results that are difficult to test against, and when converted to json, a real pain to use on the client-side. <?php $xml = new SimpleXMLElement('<foo><bar><![CDATA[]]></bar><baz/></foo>', LIBXML_NOCDATA); print_r($xml); $json = json_encode($xml); echo "{$json}\n"; ?> Since bar is empty (Even though it has a CDATA tag that is removed) before it gets to me ... I should get something like this bar => "" OR at most ... bar => SimpleXMLElemebt object () but ... what I don't expect is this: SimpleXMLElement Object ( [bar] => SimpleXMLElement Object ( [0] => ) ... When this data is converted to json ... I get bar:{"0":""} which requires me to do typeof and other types of tests on the client-side. I mentioned this issue to our resident FE PHP expert here at Y! and they said to file a bug. Reproduce code: --------------- Run this script on the command line: <?php $xml = new SimpleXMLElement('<foo><bar><![CDATA[]]></bar><baz/></foo>', LIBXML_NOCDATA); print_r($xml); $json = json_encode($xml); echo "{$json}\n"; ?> Expected result: ---------------- SimpleXMLElement Object ( [bar] => "" ) OR SimpleXMLElement Object ( [bar] => SimpleXMLElement Object ( ) ) Actual result: -------------- SimpleXMLElement Object ( [bar] => SimpleXMLElement Object ( [0] => "" ) )