I am trying to take the following array and convert it into XML. For the most part it works.
$test_array = array (
'akey' => 'auth_key',
'cmd' => 'command',
'aid' => 'account_id',
'scode' => 'short_code',
'key' => 'keyword',
'words' => 'message',
'contacts' => [
'1' => 'contact_number',
'2' => 'contact_number'
]
);
//edit: code for output
$dataXml = new SimpleXMLElement('<sms/>');
array_walk_recursive($test_array, array ($dataXml, 'addChild'));
print $dataXml->asXML();
The output is
<sms>
<auth_key>akey</auth_key>
<command>cmd</command>
<account_id>aid</account_id>
<short_code>scode</short_code><keyword>key</keyword>
<message>words</message>
<contact_number>1</contact_number>
<contact_number>2</contact_number>
</sms>
But needs to be the following
<sms>
<auth_key>akey</auth_key>
<command>cmd</command>
<account_id>aid</account_id>
<short_code>scode</short_code><keyword>key</keyword>
<message>words</message>
<contacts>
<contact_number>1</contact_number>
<contact_number>2</contact_number>
</contacts>
</sms>
I've seen solutions using DOMDocument and maybe I'll just have to go that way, but I was hoping there would be a simpler solution.
I have also tried creating contacts as it's own XML and then putting it directly into the XML, it seems like this might work beyond the tag <?xml version="1.0"?>
at the top which gets stuck right in the middle of the XML file, but even if I could manage to get rid of that I'm not sure it would even work with how specific XML needs to be.