Created
March 8, 2018 12:53
-
-
Save kobus1998/687b29f61ebd666758e6dfb0aa9f0c7c to your computer and use it in GitHub Desktop.
array to xml
This file contains hidden or 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 | |
$xml = [ | |
"payments" => [ | |
[ | |
"amount" => "100", | |
"initials" => "ih" | |
], | |
[ | |
"amount" => "1000", | |
"initials" => "ip" | |
], | |
] | |
]; | |
function arrayToXML($xml) | |
{ | |
$result = ""; | |
foreach($xml as $key => $val) | |
{ | |
if (is_array($val)) | |
{ | |
if (is_numeric($key)) | |
{ | |
$result .= "<item>" . arrayToXML($val) . "</item>"; | |
} | |
else | |
{ | |
$result .= "<$key>" . arrayToXML($val) . "</$key>"; | |
} | |
} | |
else | |
{ | |
$result .= "<$key>" . $val . "</$key>"; | |
} | |
} | |
return $result; | |
} | |
echo arrayToXML($xml); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment