{"id":70,"date":"2008-07-06T08:21:35","date_gmt":"2008-07-06T16:21:35","guid":{"rendered":"http:\/\/www.codebelay.com\/blog\/?p=70"},"modified":"2012-11-21T21:30:11","modified_gmt":"2012-11-22T05:30:11","slug":"print_r-in-php-526-doesnt-print-the-whole-simplexml-object","status":"publish","type":"post","link":"https:\/\/www.codebelay.com\/blog\/2008\/07\/06\/print_r-in-php-526-doesnt-print-the-whole-simplexml-object\/","title":{"rendered":"print_r in PHP-5.2.6 Doesn&#8217;t Print the Whole SimpleXML Object"},"content":{"rendered":"<p>There&#8217;s an interesting SimpleXML related bug in PHP 5.2.6 . print_r is unable to print all the contents of a SimpleXML object. <a href=\"http:\/\/bugs.php.net\/bug.php?id=44973\">The bug is reported on the php.net site.<\/a><\/p>\n<p>The reproduction of the code is below:<\/p>\n<pre>Reproduce code:\r\n---------------\r\n&lt;?php\r\n$xmlstr=&lt;&lt;&lt;EOXML\r\n&lt;?xml version='1.0' standalone='yes'?&gt;l;\r\n&lt;products&gt;\r\n    &lt;product order_id=\"0001\"&gt;&lt;\/product&gt;\r\n    &lt;product order_id=\"0002\"&gt;PHP book&lt;\/product&gt;\r\n    &lt;product order_id=\"0003\"&gt;\r\n        &lt;name&gt;PHP book&lt;\/name&gt;\r\n    &lt;\/product&gt;\r\n&lt;\/products&gt;\r\nEOXML;\r\n\r\n$xml=new SimpleXMLElement($xmlstr);\r\nprint_r($xml);\r\n?&gt;\r\n\r\nExpected result:\r\n----------------\r\n@attributes array for second item as well\r\n\r\nActual result:\r\n--------------\r\nSimpleXMLElement Object\r\n(\r\n    [product] =&gt; Array\r\n        (\r\n            [0] =&gt; SimpleXMLElement Object\r\n                (\r\n                    [@attributes] =&gt; Array\r\n                        (\r\n                            [order_id] =&gt; 0001\r\n                        )\r\n\r\n                )\r\n\r\n            [1] =&gt; PHP book\r\n            [2] =&gt; SimpleXMLElement Object\r\n                (\r\n                    [@attributes] =&gt; Array\r\n                        (\r\n                            [order_id] =&gt; 0003\r\n                        )\r\n\r\n                    [name] =&gt; PHP book\r\n                )\r\n\r\n        )\r\n\r\n)\r\n\r\n[13 May 1:29pm UTC] felipe@php.net\r\n\r\nThe attribute can be accessed, however toString really doesn't show it.\r\n\r\nprint_r($xml-&gt;product[1]);\r\n\r\nSimpleXMLElement Object\r\n(\r\n    [@attributes] =&gt; Array\r\n        (\r\n            [order_id] =&gt; 0002\r\n        )\r\n\r\n    [0] =&gt; PHP book\r\n)<\/pre>\n<p>It&#8217;s been pretty interesting going through the code in php-5.2.6\/ext\/simplexml . A note in the README file provides a clue:<\/p>\n<p>&#8220;Due to engine problems it is currently not possible to access a subelement by index 0: $object-&gt;property[0].&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There&#8217;s an interesting SimpleXML related bug in PHP 5.2.6 . print_r is unable to print all the contents of a SimpleXML object. The bug is reported on the php.net site. The reproduction of the code is below: Reproduce code: &#8212;&#8212;&#8212;&#8212;&#8212; &lt;?php $xmlstr=&lt;&lt;&lt;EOXML &lt;?xml version=&#8217;1.0&#8242; standalone=&#8217;yes&#8217;?&gt;l; &lt;products&gt; &lt;product order_id=&#8221;0001&#8243;&gt;&lt;\/product&gt; &lt;product order_id=&#8221;0002&#8243;&gt;PHP book&lt;\/product&gt; &lt;product order_id=&#8221;0003&#8243;&gt; &lt;name&gt;PHP book&lt;\/name&gt; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[56],"tags":[97,391,96],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-php","tag-bug","tag-php","tag-simplexml"],"_links":{"self":[{"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":0,"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codebelay.com\/blog\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}