Skip to content

Instantly share code, notes, and snippets.

@goreilly
Created September 4, 2015 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goreilly/6c3881184b64d9a71e88 to your computer and use it in GitHub Desktop.
Save goreilly/6c3881184b64d9a71e88 to your computer and use it in GitHub Desktop.
/**
* Note: Also removes anything after the _ in the key. Useful for
* adding nodes with the same name. e.g. Node_1 and Node_2 both become Node.
* Appends child to argument by reference.
* @param array $element
* @param \SimpleXMLElement $parent
* @return \SimpleXMLElement
*/
protected function arrayToXml (array $element, \SimpleXMLElement $parent) {
foreach ($element as $key => $child) {
// Removes text after first _
$key = explode('_', $key)[0];
if (is_array($child)) {
$this->arrayToXml($child, $parent->addChild($key));
} else {
$parent->addChild($key, $child);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment