3v4l.org

run code in 300+ PHP versions simultaneously
<?php $xmlStr = <<<XML <?xml version='1.0' encoding='UTF-8' standalone='yes'?> <root></root> XML; $xml = new \SimpleXMLElement($xmlStr); $xml = assocToXml(['one' => 1, 'two' => ['three' => 3, 'four' => '4']], $xml); var_dump($xml); function assocToXml(array $array, \SimpleXMLElement $xml) { foreach ($array as $key => $value) { if (!is_array($value)) { if (is_string($key)) { $xml->addChild($key, $value); } } else { $xml->addChild($key); // $xml->{$key} = $value; assocToXml($value, $xml->{$key}); } } return $xml; }

preferences:
47.58 ms | 402 KiB | 5 Q