Skip to content

Instantly share code, notes, and snippets.

@kobus1998
Created March 8, 2018 12:53
Show Gist options
  • Save kobus1998/687b29f61ebd666758e6dfb0aa9f0c7c to your computer and use it in GitHub Desktop.
Save kobus1998/687b29f61ebd666758e6dfb0aa9f0c7c to your computer and use it in GitHub Desktop.
array to xml
<?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