- var_dump: documentation ( source)
<?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;
}