John Davidson

How to output array with subarrays with same repeating element into XML with PHP

0 comments
Message:


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.



My Answer:
Here is a solution using DOMDocument to achieve the desired XML output:

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

$dataXml = new DOMDocument('1.0', 'UTF-8');
$sms = $dataXml->createElement('sms');

foreach ($test_array as $key => $value) {
if (is_array($value)) {
$contacts = $dataXml->createElement('contacts');
foreach ($value as $contact) {
$contactNode = $dataXml->createElement('contact_number', $contact);
$contacts->appendChild($contactNode);
}
$sms->appendChild($contacts);
} else {
$node = $dataXml->createElement($key, $value);
$sms->appendChild($node);
}
}

$dataXml->appendChild($sms);
$dataXml->formatOutput = true;
echo $dataXml->saveXML();


This code will output the following XML:

xml


akey
cmd
aid
scode
key
words

1
2




This code creates a DOMDocument object and then iterates over the test_array. If the value is an array, it creates a 'contacts' element and appends 'contact_number' elements for each value in the subarray. Otherwise, it creates a regular element with the key and value from the test_array. Finally, it appends all the elements to the 'sms' element and outputs the XML.

Rate this post

4 of 5 based on 8438 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us