-
-
Save cwerner1/3027220 to your computer and use it in GitHub Desktop.
PHP - ArrayToXML Converter
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 | |
function arrayToXML($array,$parent,$xml=null){ | |
if($xml == null){ | |
$xml = new SimpleXMLElement("<".$parent."/>"); | |
$child = $xml; | |
}else{ | |
$child = $xml->addChild($parent); | |
} | |
if(gettype($array) == 'array'){ | |
reset($array); | |
if(gettype(key($array)) == 'integer') unset($xml->$parent); | |
foreach($array as $key=>$element){ | |
if(gettype($key) == 'integer'){ | |
$xml = arrayToXML($element,$parent,$xml); | |
}else{ | |
$child = arrayToXML($element,$key,$child); | |
} | |
} | |
}else{ | |
$xml->$parent = $array; | |
} | |
return $xml; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment