3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php function arrayToXml($array, \SimpleXMLElement $xml) { foreach ($array as $key => $value) { if (is_array($value)) { if (!is_numeric($key)) { $subNode = $xml->addChild($key); } else { $subNode = $xml->addChild('item' . $key); } arrayToXml($value, $subNode); } else { $xml->addChild($key, htmlspecialchars($value)); } } } $array = [ 'key' => 'value', 'arrKey' => [ 'subkey1' => 1, 'subkey2' => 2, ], 0 => 'numeric zero', 1 => 'another', 2 => [ 'adin', 'two', 'three' ], ]; $xml = new \SimpleXMLElement('<response/>'); arrayToXml($array, $xml); echo $xml->asXML(); echo PHP_EOL; $xml = new SimpleXMLElement('<response/>'); array_walk_recursive(array_flip($array), array ($xml, 'addChild')); print $xml->asXML();
based on DosIc
Output for 7.0.0 - 7.2.0
<?xml version="1.0"?> <response><key>value</key><arrKey><subkey1>1</subkey1><subkey2>2</subkey2></arrKey><0>numeric zero</0><1>another</1><item2><0>adin</0><1>two</1><2>three</2></item2></response> Warning: array_flip(): Can only flip STRING and INTEGER values! in /in/KZ0JI on line 41 Warning: array_flip(): Can only flip STRING and INTEGER values! in /in/KZ0JI on line 41 Notice: Only variables should be passed by reference in /in/KZ0JI on line 41 <?xml version="1.0"?> <response><key>value</key><0>numeric zero</0><1>another</1></response>
Output for 5.4.1 - 5.6.28
<?xml version="1.0"?> <response><key>value</key><arrKey><subkey1>1</subkey1><subkey2>2</subkey2></arrKey><0>numeric zero</0><1>another</1><item2><0>adin</0><1>two</1><2>three</2></item2></response> Warning: array_flip(): Can only flip STRING and INTEGER values! in /in/KZ0JI on line 41 Warning: array_flip(): Can only flip STRING and INTEGER values! in /in/KZ0JI on line 41 Strict Standards: Only variables should be passed by reference in /in/KZ0JI on line 41 <?xml version="1.0"?> <response><key>value</key><0>numeric zero</0><1>another</1></response>
Output for 5.3.18 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/KZ0JI on line 19
Process exited with code 255.