Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save raihan-uddin/c24ed7b4aef246a8b054374969600943 to your computer and use it in GitHub Desktop.
Save raihan-uddin/c24ed7b4aef246a8b054374969600943 to your computer and use it in GitHub Desktop.
PHP : Json OR Array To XML Conversion
<?php
function arrayToXml($array, $rootElement = null, $xml = null) {
$_xml = $xml;
if ($_xml === null) {
$_xml = new SimpleXMLElement($rootElement !== null ? $rootElement : '<root><root/>');
}
foreach ($array as $k => $v) {
if (is_array($v)) { //nested array
arrayToXml($v, $k, $_xml->addChild($k));
} else {
$_xml->addChild($k, $v);
}
}
return $_xml->asXML();
}
$str = '{ "account_id" : "1",
"destination_number" : "09824326809",
"login_id" : "demo1",
"message" : "Hello",
"password_hash" : "demo123",
"return_address" : "prakash@inheritx.com"
}';
$array = json_decode($str,true);
$xml = arrayToXml($array, '<sendsms></sendsms>');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment