3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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.5.27 - 5.5.38, 5.6.11 - 7.2.0
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 )
Output for 5.4.0 - 5.5.26, 5.6.0 - 5.6.10
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 )