3v4l.org

run code in 300+ PHP versions simultaneously
<?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 { $xml->addChild($key, $value); /* if (strpos($value, '<') === false) { $xml->addChild($key, $value); } else { $xml->addChild($key)->addCDATA($value); } */ } } }
Output for git.master, git.master_jit, rfc.property-hooks
Fatal error: Uncaught TypeError: SimpleXMLElement::addChild(): Argument #2 ($value) must be of type ?string, DateTime given in /in/cmVIH:61 Stack trace: #0 /in/cmVIH(61): SimpleXMLElement->addChild('birthday', Object(DateTime)) #1 /in/cmVIH(16): arrayToXML(Array, Object(SimpleXMLElementExtended)) #2 {main} thrown in /in/cmVIH on line 61
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
58.32 ms | 401 KiB | 8 Q