3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $data = array( 'id' => 1, 'name' => 'Jacopo', 'male' => true, 'birthday' => new DateTime('1988-07-26'), 'hobbies' => array( 'Programming', 'TV Shows', 'Skiing' ) ); $rootXMLElement = '<data />'; $xml = new SimpleXMLElementExtended('<?xml version="1.0"?>' . $rootXMLElement); arrayToXML($data, $xml); echo $xml->asXML(); // http://coffeerings.posterous.com/php-simplexml-and-cdata class SimpleXMLElementExtended extends SimpleXMLElement { public function addCDATA($text) { $node = dom_import_simplexml($this); $no = $node->ownerDocument; $node->appendChild($no->createCDATASection($text)); } public function getParentNode() { return current($this->xpath('parent::*')); } } function arrayToXML($data, &$xml) { foreach ($data as $key => $value) { if (is_numeric($key)) { $key = 'item'; } if (is_array($value)) { if (!is_numeric($key)) { $node = $xml->addChild($key); arrayToXML($value, $node); } else { // $currentNodeName = $xml->getName(); // $currentNodeNameSingular = @Inflector::singularize($currentNodeName); // if ( $currentNodeName != 'data' // && $currentNodeName != $currentNodeNameSingular // ) { // $key = $currentNodeNameSingular; // } else { // $key = 'item'; // } $node = $xml->addChild($key); arrayToXML($value, $node); } } else { if (strpos($value, '<') === false) { $xml->addChild($key, $value); } else { $xml->addChild($key)->addCDATA($value); } } } }
Output for 7.0.0 - 7.1.0
Warning: strpos() expects parameter 1 to be string, object given in /in/YG4NO on line 61 Warning: DOMDocument::createCDATASection() expects parameter 1 to be string, object given in /in/YG4NO on line 26 Fatal error: Uncaught TypeError: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given in /in/YG4NO:26 Stack trace: #0 /in/YG4NO(26): DOMNode->appendChild(NULL) #1 /in/YG4NO(64): SimpleXMLElementExtended->addCDATA(Object(DateTime)) #2 /in/YG4NO(16): arrayToXML(Array, Object(SimpleXMLElementExtended)) #3 {main} thrown in /in/YG4NO on line 26
Process exited with code 255.
Output for hhvm-3.10.0 - 3.12.0
Warning: strpos() expects parameter 1 to be string, object given in /in/YG4NO on line 61 Warning: createCDATASection() expects parameter 1 to be string, object given in /in/YG4NO on line 26 Warning: appendChild() expects parameter 1 to be object, null given in /in/YG4NO on line 26 <?xml version="1.0"?> <data><id>1</id><name>Jacopo</name><male>1</male><birthday/><hobbies><item>Programming</item><item>TV Shows</item><item>Skiing</item></hobbies></data>
Output for 5.3.0 - 5.6.21
Warning: strpos() expects parameter 1 to be string, object given in /in/YG4NO on line 61 Warning: DOMDocument::createCDATASection() expects parameter 1 to be string, object given in /in/YG4NO on line 26 Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, null given, called in /in/YG4NO on line 64 and defined in /in/YG4NO on line 26
Process exited with code 255.
Output for 5.2.0 - 5.2.17
Catchable fatal error: Object of class DateTime could not be converted to string in /in/YG4NO on line 61
Process exited with code 255.
Output for 5.0.0 - 5.1.6
Fatal error: Class 'DateTime' not found in /in/YG4NO on line 6
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/YG4NO on line 22
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/YG4NO on line 22
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/YG4NO on line 22
Process exited with code 255.