如何将xml结果转换为数组

时间:2014-12-13 09:11:54

标签: php xml json codeigniter

<entry>
    <string>actiontypedesc</string>
    <string>Registration of google1231.com for 1 year</string>
</entry>
<entry>
    <string>unutilisedsellingamount</string>
    <string>-1531.770</string>
</entry>
<entry>
    <string>sellingamount</string>
    <string>-1531.770</string>
</entry>
<entry>
    <string>entityid</string>
    <string>57928339</string>
</entry>
<entry>
    <string>actionstatus</string>
    <string>Success</string>
</entry>`

这是API调用后的输出如何将其转换为数组?

1 个答案:

答案 0 :(得分:4)

可以尝试使用simplexml_load_string()。用父标记包含整个xml。我在这里加入了<myxml>...</myxml>。例如:

$myXMLData =
"<?xml version='1.0' encoding='UTF-8'?>
<myxml>
<entry>
    <string>actiontypedesc</string>
    <string>Registration of google1231.com for 1 year</string>
</entry>
<entry>
    <string>unutilisedsellingamount</string>
    <string>-1531.770</string>
</entry>
<entry>
    <string>sellingamount</string>
    <string>-1531.770</string>
</entry>
<entry>
    <string>entityid</string>
    <string>57928339</string>
</entry>
<entry>
    <string>actionstatus</string>
    <string>Success</string>
</entry>
</myxml>";

$xml = json_decode(json_encode(simplexml_load_string($myXMLData)), true);
// json_encode() & json_decode() is to convert object to array
print '<pre>';
print_r($xml);
print '</pre>';