[eluser]Kamarg[/eluser]
I use this. You'll need to replace append_child, create_element, and create_text_node with your own xml creation functions.
Code:
function _arrayToXml($parent, $children, $root_node_name='data') {
$numeric = FALSE;
// loop through the data passed in.
foreach($children as $key => $value) {
// no numeric keys per xml spec. make string a key
if(is_numeric($key)) {
$numeric = TRUE;
$key = $root_node_name;
}
// replace anything not alpha numeric/underscore
$key = preg_replace('/[^a-z0-9_]/i', '', $key);
$node = create_element($this->_dom, $key);
// if there is another array found, recursively call this function
if(is_array($value)) {
if($numeric || is_assoc($value)) {
append_child($parent, $node);
$this->_arrayToXml($node, $value, $key);
} else {
$this->_arrayToXml($parent, $value, $key);
}
} else {
// add single node
$text_node = create_text_node($this->_dom, $value);
append_child($node, $text_node);
append_child($parent, $node);
}
}
}
function is_assoc($array) {
return (is_array($array) && (0 !== count(array_diff_key($array, array_keys(array_keys($array)))) || count($array)==0));
}