【发布时间】:2016-10-11 01:44:05
【问题描述】:
我得到了复杂的 XML 作为响应,我需要将其转换为数组,这是我的示例复杂 XML
<?xml version="1.0" encoding="UTF-8"?>
<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SearchResult>
<Result>
<aaa>string</aaa>
<bbb>string</bbb>
<ccc>
<ddd>
<eee>string</eee>
<fff>string</fff>
</ddd>
<ddd>
<eee>string</eee>
<fff>string</fff>
</ddd>
</ccc>
<ggg>
<hhh>
<iii>string</iii>
<eee>string</eee>
<jjj>string</jjj>
</hhh>
<hhh>
<iii>string</iii>
<eee>string</eee>
<jjj>string</jjj>
</hhh>
</ggg>
<kkk>
<hhh>
<iii>string</iii>
<eee>string</eee>
<jjj>string</jjj>
</hhh>
</kkk>
<lll>
<mmm>string</mmm>
<nnn>string</nnn>
<ooo>string</ooo>
</lll>
</Result>
<Result>
//same values as above
</Result>
<Result>
//same values as above
</Result>
</SearchResult>
</Response>
我尝试过简单的 XML,但它不起作用,
function xml2Array($xmlstring)
{
$xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
return json_decode($json,TRUE);
}
$arr = xml2Array($response);
print_r($arr);
但它返回空数组,请提出可能的解决方案
【问题讨论】:
-
您可能想先阅读本文stackoverflow.com/q/6578832/80836
-
在本地系统中为我显示正确的数组?检查你的错误日志
标签: php arrays xml codeigniter