- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
function arr2xml($data, $rootNodeName = 'response', $xml = null) {
if ($xml == null) {
$xml = simplexml_load_string("<$rootNodeName />");
}
// loop through the data passed in.
foreach($data as $key => $value) {
// no numeric keys in our xml please!
if (is_numeric($key)) {
// make string key...
$key = "unknownNode_". (string) $key;
}
// replace anything not alpha numeric
$key = preg_replace('/[^a-z_0-9]/i', '', $key);
// if there is another array found recrusively call this function
if (is_array($value)) {
$node = $xml->addChild($key);
// recrusive call.
arr2xml($value, $rootNodeName, $node);
} else {
// add single node.
$value = $value;
$xml->addChild($key,$value);
}
}
// pass back as string. or simple xml object if you want!
$res = substr($xml->asXML(), 22);
return $res;
}
Комментарии (0) RSS
Добавить комментарий