3v4l.org

run code in 300+ PHP versions simultaneously
<?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();
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
<?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, entry skipped in /in/KZ0JI on line 41 Warning: array_flip(): Can only flip string and integer values, entry skipped 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 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.31, 7.4.0 - 7.4.33
<?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 7.3.32 - 7.3.33
<?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 <?xml version="1.0"?> <response><key>value</key><0>numeric zero</0><1>another</1></response>
Output for 5.4.1 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 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>

preferences:
204.92 ms | 403 KiB | 227 Q