3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function arrayToXml($data, \SimpleXmlElement $element, $parent = null) { echo 'data: '; var_dump($data) ; echo $element->asXml(); echo 'parent: '.$parent; foreach ($data as $key => $value) { if (is_array($value) || $value instanceof \Traversable) { if (!is_numeric($key)) { if (count($value) > 0 && isset($value[0])) { arrayToXml($value, $element, $key); } else { $subnode = $element->addChild($key); arrayToXml($value, $subnode, $key); } } else { $subnode = $element->addChild($parent); arrayToXml($value, $subnode, $parent); } } else { if (!is_numeric($key)) { if (substr($key, 0, 1) === '@') { $element->addAttribute(substr($key, 1), $value); } elseif ($key === 'value' and count($data) === 1) { $element->{0} = $value; } elseif (is_bool($value)) { $element->addChild($key, intval($value)); } else { $element->addChild($key, htmlspecialchars($value, ENT_QUOTES)); } } else { $element->addChild($parent, htmlspecialchars($value, ENT_QUOTES)); } } } } $data = array('x' => array('value' => 'test')); $doc = new \SimpleXMLElement('<resource href="/"></resource>'); arrayToXml($data, $doc); echo $doc->asXml(); //$xml = new \SimpleXMLElement('<resource href="/"><x><0>test</0></x></resource>'); //echo $xml->x;
Output for 7.0.0 - 7.1.0
data: array(1) { ["x"]=> array(1) { ["value"]=> string(4) "test" } } <?xml version="1.0"?> <resource href="/"/> parent: data: array(1) { ["value"]=> string(4) "test" } <x/>parent: x<?xml version="1.0"?> <resource href="/"><x><0>test</0></x></resource>
Output for 5.3.0 - 5.6.28
data: array(1) { ["x"]=> array(1) { ["value"]=> string(4) "test" } } <?xml version="1.0"?> <resource href="/"/> parent: data: array(1) { ["value"]=> string(4) "test" } <x/>parent: x<?xml version="1.0"?> <resource href="/"><x>test</x></resource>
Output for 5.2.2 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fnMUr on line 2 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fnMUr on line 6 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fnMUr on line 37 data: array(1) { ["x"]=> array(1) { ["value"]=> string(4) "test" } } <?xml version="1.0"?> <resource href="/"/> parent: data: array(1) { ["value"]=> string(4) "test" } <x/>parent: x<?xml version="1.0"?> <resource href="/"><x>test</x></resource>
Output for 5.1.3 - 5.2.1
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fnMUr on line 2 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fnMUr on line 6 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fnMUr on line 37 data: array(1) { ["x"]=> array(1) { ["value"]=> string(4) "test" } } <?xml version="1.0"?> <resource href="/"/> parent: data: array(1) { ["value"]=> string(4) "test" } <x/>parent: x<?xml version="1.0"?> <resource href="/"><x/><x>test</x></resource>
Output for 5.0.0 - 5.0.5, 5.1.1 - 5.1.2
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fnMUr on line 2 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fnMUr on line 6 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fnMUr on line 37 data: array(1) { ["x"]=> array(1) { ["value"]=> string(4) "test" } } <?xml version="1.0"?> <resource href="/"/> parent: Fatal error: Call to undefined method SimpleXMLElement::addChild() in /in/fnMUr on line 11
Process exited with code 255.
Output for 5.1.0
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fnMUr on line 2 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fnMUr on line 6 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fnMUr on line 37 Fatal error: fatal flex scanner internal error--end of buffer missed in /in/fnMUr on line 43
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fnMUr on line 2 Parse error: syntax error, unexpected T_STRING, expecting '&' or T_VARIABLE or T_CONST in /in/fnMUr on line 2
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fnMUr on line 2 Parse error: parse error, unexpected T_STRING, expecting '&' or T_VARIABLE or T_CONST in /in/fnMUr on line 2
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/fnMUr on line 2 Parse error: parse error, expecting `'&'' or `T_VARIABLE' or `T_CONST' in /in/fnMUr on line 2
Process exited with code 255.