3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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);
based on GC0MO
Output for 5.3.0 - 5.3.29, 5.5.27 - 5.5.37, 5.6.11 - 7.2.0
Array ( [SUCCESS] => true [ERRORMESSAGE] => Array ( [0] => ) [TIMESTAMP] => 21/09/2015 03:26:37 )
Output for 5.4.0 - 5.5.26, 5.6.0 - 5.6.10
Array ( [SUCCESS] => true [ERRORMESSAGE] => Array ( ) [TIMESTAMP] => 21/09/2015 03:26:37 )
Output for 5.2.2 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/lLpZu on line 20 Array ( [SUCCESS] => true [ERRORMESSAGE] => Array ( [0] => ) [TIMESTAMP] => 21/09/2015 03:26:37 )
Output for 5.2.0 - 5.2.1
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/lLpZu on line 20 Warning: _fixEmptyStringDifference(): It is not yet possible to assign complex types to properties in /in/lLpZu on line 21 Warning: _fixEmptyStringDifference(): It is not possible to assign complex types to nodes in /in/lLpZu on line 21 Array ( [SUCCESS] => true [ERRORMESSAGE] => Array ( ) [TIMESTAMP] => 21/09/2015 03:26:37 )
Output for 5.1.3 - 5.1.6
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/lLpZu on line 20 Warning: _fixEmptyStringDifference(): It is not yet possible to assign complex types to properties in /in/lLpZu on line 21 Warning: _fixEmptyStringDifference(): It is not possible to assign complex types to nodes in /in/lLpZu on line 21 Fatal error: Call to undefined function json_decode() in /in/lLpZu on line 33
Process exited with code 255.
Output for 5.0.4 - 5.1.2
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/lLpZu on line 20 Warning: _fixEmptyStringDifference(): It is not possible to assign complex types to nodes in /in/lLpZu on line 21 Fatal error: Call to undefined function json_decode() in /in/lLpZu on line 33
Process exited with code 255.
Output for 5.0.1 - 5.0.3
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/lLpZu on line 20 Warning: _fixEmptyStringDifference(): It is not possible to assign complex types to nodes in /in/lLpZu on line 21
Process exited with code 139.
Output for 5.0.0
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/lLpZu on line 20 Warning: It is not possible to assign complex types to nodes in /in/lLpZu on line 21
Process exited with code 139.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING in /in/lLpZu on line 20
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING in /in/lLpZu on line 20
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/lLpZu on line 20
Process exited with code 255.