变量$d
来自于file_get_contents
函数对一个URL的调用。
$answer = @new SimpleXMLElement($d);
以下是print_r($answer)
的输出:
SimpleXMLElement Object
(
[Amount] => 2698
[Status] => OK
[State] => FL
[Country] => USA
)
我该如何获取每个元素的值并添加到数组中?我无法理解。
$my_array = (array)$answer
这应该是有效的:
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
json_encode
和json_decode
方法遇到非UTF-8字符时,它们往往会出错。尝试用try...catch
包装可能是明智的,但我不知道是否有一种方法可以正确处理它(即编码为UTF-8)而不编写自定义解析器。 - Joshjson_encode
和 http_build_query
都无法正确解析 <![CDATA[
]]>
标记。两个函数都将其视为空对象,导致 http_build_query
完全忽略此标记。 - Shaun Cockerill$answer
已经可以作为一个数组来使用。如果你希望将其放入一个真正的数组中,你可以这样做。
$array = array();
foreach($answer as $k => $v) {
$array[$k] = $v;
}
我对这个函数有些问题,因为将每个XML子元素强制转换为数组可能会在文本位于CDATA
标记之间时出现问题。
我通过检查将子元素强制转换为数组的结果是否为空来解决了这个问题。如果是,那么将它强制转换为字符串,就可以得到正确的结果。
以下是我的修改版本,支持CDATA
:
function SimpleXML2ArrayWithCDATASupport($xml)
{
$array = (array)$xml;
if (count($array) === 0) {
return (string)$xml;
}
foreach ($array as $key => $value) {
if (!is_object($value) || strpos(get_class($value), 'SimpleXML') === false) {
continue;
}
$array[$key] = SimpleXML2ArrayWithCDATASupport($value);
}
return $array;
}
json_encode
和http_build_query
都有相同的症状,因此这种方法是最可靠的。 - Shaun Cockerill$xml = simplexml_load_file('file.xml',SimpleXMLElement::class,LIBXML_NOCDATA);
,但是然后您的函数就不再需要了,我可以只做json_decode(json_encode((array) $xml),true);
。 - Rodrigofunction SimpleXML2Array($xml){
$array = (array)$xml;
//recursive Parser
foreach ($array as $key => $value){
if(strpos(get_class($value),"SimpleXML")!==false){
$array[$key] = SimpleXML2Array($value);
}
}
return $array;
}