转换一个数组XML或JSON [英] Convert an array to XML or JSON
本文介绍了转换一个数组XML或JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要低于
Array
(
[city] => Array
(
[0] => Array
(
[0] => Rd
[1] => E
)
[1] => B
[2] => P
[3] => R
[4] => S
[5] => G
[6] => C
)
[dis] => 1.4
)
成XML格式或JSON。有人可以帮助吗?
into XML format or JSON. Someone may help please?
推荐答案
这适用于关联数组。
function array2xml($array, $node_name="root") {
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$root = $dom->createElement($node_name);
$dom->appendChild($root);
$array2xml = function ($node, $array) use ($dom, &$array2xml) {
foreach($array as $key => $value){
if ( is_array($value) ) {
$n = $dom->createElement($key);
$node->appendChild($n);
$array2xml($n, $value);
}else{
$attr = $dom->createAttribute($key);
$attr->value = $value;
$node->appendChild($attr);
}
}
};
$array2xml($root, $array);
return $dom->saveXML();
}
这篇关于转换一个数组XML或JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文