php 将数组转换为 XML 或 JSON
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9152176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert an array to XML or JSON
提问by known reg
I want to convert the array below
我想转换下面的数组
Array
(
[city] => Array
(
[0] => Array
(
[0] => Rd
[1] => E
)
[1] => B
[2] => P
[3] => R
[4] => S
[5] => G
[6] => C
)
[dis] => 1.4
)
into XML format or JSON. Someone may help please?
转换成 XML 格式或 JSON。有人可以帮忙吗?
回答by nickb
JSON, use the json_encodefunction:
JSON,使用json_encode函数:
<?php echo json_encode( $array); ?>
XML, see this question.
XML,请参阅此问题。
回答by Akan Nkweini
This works for associative arrays.
这适用于关联数组。
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();
}
回答by joelkema
Which programming language are you using ?
您使用哪种编程语言?
In case you are using PHP you can use the following to convert to JSON:
如果您使用的是 PHP,您可以使用以下内容转换为 JSON:
$json = json_encode($your_array);
And for XML you can check the following answer: How to convert array to SimpleXML.
对于 XML,您可以查看以下答案:How to convert array to SimpleXML。
Hope it helps.
希望能帮助到你。
回答by Jeffz
NOTE:numbers for XML element name is not a good idea, so $your_array should not have numbers for keys.
注意:XML 元素名称的数字不是一个好主意,所以 $your_array 不应该有键的数字。
Try this:
尝试这个:
$your_array = array( 'city' => array ( '0' => array('0' => 'Rd', '1' => 'E'), '1' => 'B', '2' => 'P', '3' => 'R', '4' => 'S', '5' => 'G', '6' => 'C' ), 'dis' => '1.4' );
Function below calls itself (recursion), so it should work for array of any depth.
下面的函数调用自身(递归),因此它应该适用于任何深度的数组。
Function uses ternary operator:
函数使用三元运算符:
(condition) ? if true action : if false action
... to check if value called is array.
...检查调用的值是否为数组。
If it is array, it calls itself (recursion) to dig deeper, if value is not an array, it is being appended to XML object, using array key for element name and array value for element value.
如果是数组,则调用自身(递归)进行深入挖掘,如果value不是数组,则将其附加到XML对象中,元素名称使用数组键,元素值使用数组值。
function array_to_xml(array $your_array, SimpleXMLElement $xml){ foreach ($arr as $k => $v){ is_array($v) ? array_to_xml($v, $xml->addChild($k)) : $xml->addChild($k, $v); } return $xml; } $your_xml = $this->array_to_xml($your_array, new SimpleXMLElement(''))->asXML();
Now, your array is an XML and is enclosed in $your_xml variable, so you can with it whatever you want.
现在,您的数组是一个 XML 并包含在 $your_xml 变量中,因此您可以随心所欲地使用它。
$your_xml output (e.g. if you 'echo' it) would look like this:
$your_xml 输出(例如,如果您“回显”它)将如下所示:
<root> <city> <0> <0>Rd</0> <1>E</1> </0> <1>B</1> <2>P</2> <3>R</3> <4>S</4> <5>G</5> <6>C</6> </city> <dis>1.4</dis> </root>