3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(E_ALL); global $n; $n=0; 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, $z=0) { foreach (array_keys((array)$simpleXmlObj) as $key => $element) { $el = (array)$simpleXmlObj->$element; if(isset($el[0])){ print_r($el[0]); $trim = trim($el[0]); echo '>>>';print_r($trim);echo '<<<'; if (empty($trim) && $trim !== 0) { unset($simpleXmlObj->$element); } }else{ $simpleXmlObj->$element = _fixEmptyStringDifference($simpleXmlObj->$element, $z+1); } } 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);
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.26, 5.6.0 - 5.6.10, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
true>>>true<<<SimpleXMLElement Object ( ) >>><<<21/09/2015 03:26:37>>>21/09/2015 03:26:37<<<Array ( [SUCCESS] => true [TIMESTAMP] => 21/09/2015 03:26:37 )
Output for 5.5.27 - 5.5.38, 5.6.11 - 5.6.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
true>>>true<<<SimpleXMLElement Object ( [0] => ) >>><<<21/09/2015 03:26:37>>>21/09/2015 03:26:37<<<Array ( [SUCCESS] => true [TIMESTAMP] => 21/09/2015 03:26:37 )

preferences:
233.58 ms | 403 KiB | 340 Q