0

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.

Marcus
  • 51
  • 7
  • Please post the code you used to make the first output. – Chris Haas Jul 04 '21 at 13:58
  • Oops, sorry, I totally spaced that. – Marcus Jul 04 '21 at 14:01
  • 1
    There is https://stackoverflow.com/a/3289602/1213708, but it assumes the data is the 'standard' way round (i.e. `'akey' => 'auth_key',` would be `'auth_key' => 'akey',`) – Nigel Ren Jul 04 '21 at 14:15
  • Thanks, this partially solves the problem. The problem now would be that when the data is flipped to the standard way the `contact_number` overwrites itself for each number and the output becomes`2` and contact 1 is gone. – Marcus Jul 04 '21 at 14:27
  • This did get me to the solution though. I took that array and flipped the $key and $value in the add child and kept the array values flipped as in my original question. This gave me the exact output I needed. – Marcus Jul 04 '21 at 14:32

1 Answers1

0

I was able to come to the answer with @Nigel Ren 's help and the answer they referred to https://stackoverflow.com/a/3289602/1213708.

<?php
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($v, $k); // this is the important part where the $key/$value is flipped and fixes the issue I was having
    }
    return $xml;
}

$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'
  ]
);

echo array_to_xml($test_array, new SimpleXMLElement('<sms/>'))->asXML();
Marcus
  • 51
  • 7