3v4l.org

run code in 300+ PHP versions simultaneously
<?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);
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.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Array ( [SUCCESS] => true [ERRORMESSAGE] => Array ( ) [TIMESTAMP] => 21/09/2015 03:26:37 )
Output for 5.3.0 - 5.3.29, 5.5.27 - 5.5.38, 5.6.11 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
Array ( [SUCCESS] => true [ERRORMESSAGE] => Array ( [0] => ) [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.0.5, 5.1.0 - 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.3.11, 4.4.0 - 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.

preferences:
360.8 ms | 401 KiB | 455 Q