Forked from vrushank-snippets/PHP : Json OR Array To XML Conversion
Created
March 14, 2020 09:41
-
-
Save raihan-uddin/c24ed7b4aef246a8b054374969600943 to your computer and use it in GitHub Desktop.
PHP : Json OR Array To XML Conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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