Tasked with programming a class for an API to send out XML POST requests and receive an answer. Task is easy but a simple problem occurs when one of the calls includes an XML as follows:
<?xml version="1.0" encoding="UTF-8"?>
<request>
<param>
<paramkey>value</paramkey>
<paramkey>value</paramkey>
</param>
</request>
I have an automated method to transform array to xml:
private function array_to_xml(array $arr, SimpleXMLElement $xml)
{
foreach ($arr as $k => $v) {
is_array($v)
? $this->array_to_xml($v, $xml->addChild($k))
: $xml->addChild($k, $v);
}
return $xml;
}
The problem is with two paramkey elements meaning PHP would have to create two array keys with the same name and it refuses to do this. The latter overwrites the first. Is there a way to create the paramkey elements in a loop without the code discarding any of them?
No comments:
Post a Comment