Last active
September 20, 2016 13:57
-
-
Save phaberest/763a14a9b15fbb6711addd40a15d668b to your computer and use it in GitHub Desktop.
[PHP] Array to XML
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
private function arrayToXML($data, &$xml) | |
{ | |
foreach ($data as $key => $value) { | |
if (is_array($value)) { | |
if (is_numeric($key)) { | |
$key = 'item'.$key; //dealing with <0/>..<n/> issues | |
} | |
$subnode = $xml->addChild($key); | |
array_to_xml($value, $subnode); | |
} else { | |
$xml->addChild("$key", htmlspecialchars("$value")); | |
} | |
} | |
} | |
private function prepareXML(array $array) | |
{ | |
$xml = new SimpleXMLElement('<?xml version="1.0"?><data></data>'); | |
$this->array_to_xml($data, $xml); | |
return $xml; # Get XML with $->asXML(), save it with $->asXML({path}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment