- json_decode: documentation ( source)
- simplexml_load_string: documentation ( source)
- print_r: documentation ( source)
- get_object_vars: documentation ( source)
- json_encode: documentation ( source)
<?php
function convert($xml)
{
$simpleXmlObj = simplexml_load_string($xml);
$simpleXmlObj = _fixVersionDifferences($simpleXmlObj);
$array = _simpleXmlObj2Array($simpleXmlObj);
return $array;
}
function _fixVersionDifferences($simpleXmlObj)
{
$simpleXmlObj = _fixEmptyStringDifference($simpleXmlObj);
return $simpleXmlObj;
}
function _fixEmptyStringDifference($simpleXmlObj)
{
foreach (get_object_vars($simpleXmlObj) as $key => $element) {
if ($element instanceof \SimpleXMLElement) {
$simpleXmlObj->$key = _fixEmptyStringDifference($element);
}
if ($element != 0 && empty($element)) {
unset($simpleXmlObj->$key);
}
}
return $simpleXmlObj;
}
function _simpleXmlObj2Array($simpleXmlObj)
{
//Maybe it is not fast but the easiest way to convert
return json_decode(json_encode($simpleXmlObj), TRUE);
}
$xml = <<<xml
<?xml version="1.0" encoding="utf-8"?>
<RESPONSE>
<SUCCESS>true</SUCCESS>
<ERRORMESSAGE>
</ERRORMESSAGE>
<TIMESTAMP>21/09/2015 03:26:37</TIMESTAMP>
</RESPONSE>
xml;
$response = convert($xml);
print_r($response);