Farooq Khan Farooq Khan - 2 months ago 10
JSON Question

how to convert xml result into array

<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>`


this is the output after the API call how do i convert it into the array?

Answer

Can try using simplexml_load_string(). Enclose your whole xml by a parent tag. Here I included <myxml>...</myxml>. Example:

$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>';
Comments