3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php $xml_str = '<?xml version="1.0" encoding="UTF-8" standalone="no"?> <ExpResponse engineVersion="201404_02a" status="OK" xml:space="preserve"> <MethodReturn> <TrainInformation displayName="普通" driveComment="平日運転" guideComment="" traffic="1" type="Train"> <StopStationList> <StopStation areaCode="16" code="29181" departureTime="1810" getOff="False" getOn="False" kenCode="26" name="国際会館"/> <StopStation areaCode="16" arraivalTime="1812" arrivalTime="1812" code="29182" departureTime="1812" getOff="False" getOn="False" kenCode="26" name="松ケ崎(京都府)"/> <StopStation areaCode="16" arraivalTime="1814" arrivalTime="1814" code="25641" departureTime="1814" getOff="False" getOn="False" kenCode="26" name="北山(京都府)"/> <StopStation areaCode="16" arraivalTime="1816" arrivalTime="1816" code="25639" departureTime="1816" getOff="False" getOn="False" kenCode="26" name="北大路"/> <StopStation areaCode="16" arraivalTime="1818" arrivalTime="1818" code="25656" departureTime="1818" getOff="False" getOn="False" kenCode="26" name="鞍馬口"/> <StopStation areaCode="16" arraivalTime="1819" arrivalTime="1819" code="25598" departureTime="1820" getOff="False" getOn="False" kenCode="26" name="今出川"/> <StopStation areaCode="16" arraivalTime="1822" arrivalTime="1822" code="25767" departureTime="1822" getOff="False" getOn="False" kenCode="26" name="丸太町(京都市営)"/> <StopStation areaCode="16" arraivalTime="1824" arrivalTime="1824" code="25607" departureTime="1824" getOff="False" getOn="False" kenCode="26" name="烏丸御池"/> <StopStation areaCode="16" arraivalTime="1825" arrivalTime="1825" code="25681" departureTime="1826" getOff="False" getOn="False" kenCode="26" name="四条(京都市営)"/> <StopStation areaCode="16" arraivalTime="1827" arrivalTime="1827" code="25670" departureTime="1828" getOff="False" getOn="False" kenCode="26" name="五条(京都市営)"/> <StopStation areaCode="16" arraivalTime="1829" arrivalTime="1829" code="25647" departureTime="1830" getOff="False" getOn="False" kenCode="26" name="京都"/> <StopStation areaCode="16" arraivalTime="1831" arrivalTime="1831" code="25651" departureTime="1831" getOff="False" getOn="False" kenCode="26" name="九条(京都府)"/> <StopStation areaCode="16" arraivalTime="1832" arrivalTime="1832" code="25694" departureTime="1833" getOff="False" getOn="False" kenCode="26" name="十条(京都市営)"/> <StopStation areaCode="16" arraivalTime="1834" arrivalTime="1834" code="25650" departureTime="1835" getOff="False" getOn="False" kenCode="26" name="くいな橋"/> <StopStation areaCode="16" arraivalTime="1836" arrivalTime="1836" code="25701" departureTime="1838" getOff="False" getOn="False" kenCode="26" name="竹田(京都府)"/> <StopStation areaCode="16" arraivalTime="1840" arrivalTime="1840" code="25752" departureTime="1840" getOff="False" getOn="False" kenCode="26" name="伏見(京都府)"/> <StopStation areaCode="16" arraivalTime="1842" arrivalTime="1842" code="25648" departureTime="1842" getOff="False" getOn="False" kenCode="26" name="近鉄丹波橋"/> <StopStation areaCode="16" arraivalTime="1843" arrivalTime="1843" code="25780" departureTime="1844" getOff="False" getOn="False" kenCode="26" name="桃山御陵前"/> <StopStation areaCode="16" arraivalTime="1847" arrivalTime="1847" code="25776" departureTime="1847" getOff="False" getOn="False" kenCode="26" name="向島"/> <StopStation areaCode="16" arraivalTime="1850" arrivalTime="1850" code="25618" departureTime="1851" getOff="False" getOn="False" kenCode="26" name="小倉(京都府)"/> <StopStation areaCode="16" arraivalTime="1852" arrivalTime="1852" code="25594" departureTime="1852" getOff="False" getOn="False" kenCode="26" name="伊勢田"/> <StopStation areaCode="16" arraivalTime="1854" arrivalTime="1854" code="25614" departureTime="1859" getOff="False" getOn="False" kenCode="26" name="大久保(京都府)"/> <StopStation areaCode="16" arraivalTime="1900" arrivalTime="1900" code="25653" departureTime="1901" getOff="False" getOn="False" kenCode="26" name="久津川"/> <StopStation areaCode="16" arraivalTime="1902" arrivalTime="1902" code="25716" departureTime="1902" getOff="False" getOn="False" kenCode="26" name="寺田(京都府)"/> <StopStation areaCode="16" arraivalTime="1904" arrivalTime="1904" code="25722" departureTime="1904" getOff="False" getOn="False" kenCode="26" name="富野荘"/> <StopStation areaCode="16" arraivalTime="1907" arrivalTime="1907" code="25690" getOff="False" getOn="False" kenCode="26" name="新田辺"/> </StopStationList> </TrainInformation> </MethodReturn> </ExpResponse>'; $xml = simplexml_load_string($xml_str); $result = xml_to_array($xml); // print_r($result); echo json_encode($result); function xml_to_array($xml) { $attributes = array(); $namespaces = $xml->getNameSpaces(true); if (count($namespaces) > 0) { foreach ($namespaces as $ns_key => $ns_val) { foreach ($xml->attributes($ns_val) as $key => $val) { $attributes[$ns_key.":".$key] = (string)$val; } } } foreach ($xml->attributes() as $key => $val) { $attributes[$key] = (string)$val; } $is_single = (count($xml->children()) === 1); foreach ($xml->children() as $key => $val) { $res = xml_to_array($val); if ($is_single) { $attributes[$key] = $res; } elseif (array_key_exists($key, $attributes)) { array_push($attributes[$key], $res); } else { $attributes[$key] = array($res); } } return $attributes; }
Output for 5.2.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
{"xml:space":"preserve","engineVersion":"201404_02a","status":"OK","MethodReturn":{"TrainInformation":{"displayName":"\u666e\u901a","driveComment":"\u5e73\u65e5\u904b\u8ee2","guideComment":"","traffic":"1","type":"Train","StopStationList":{"StopStation":[{"areaCode":"16","code":"29181","departureTime":"1810","getOff":"False","getOn":"False","kenCode":"26","name":"\u56fd\u969b\u4f1a\u9928"},{"areaCode":"16","arraivalTime":"1812","arrivalTime":"1812","code":"29182","departureTime":"1812","getOff":"False","getOn":"False","kenCode":"26","name":"\u677e\u30b1\u5d0e(\u4eac\u90fd\u5e9c)"},{"areaCode":"16","arraivalTime":"1814","arrivalTime":"1814","code":"25641","departureTime":"1814","getOff":"False","getOn":"False","kenCode":"26","name":"\u5317\u5c71(\u4eac\u90fd\u5e9c)"},{"areaCode":"16","arraivalTime":"1816","arrivalTime":"1816","code":"25639","departureTime":"1816","getOff":"False","getOn":"False","kenCode":"26","name":"\u5317\u5927\u8def"},{"areaCode":"16","arraivalTime":"1818","arrivalTime":"1818","code":"25656","departureTime":"1818","getOff":"False","getOn":"False","kenCode":"26","name":"\u978d\u99ac\u53e3"},{"areaCode":"16","arraivalTime":"1819","arrivalTime":"1819","code":"25598","departureTime":"1820","getOff":"False","getOn":"False","kenCode":"26","name":"\u4eca\u51fa\u5ddd"},{"areaCode":"16","arraivalTime":"1822","arrivalTime":"1822","code":"25767","departureTime":"1822","getOff":"False","getOn":"False","kenCode":"26","name":"\u4e38\u592a\u753a(\u4eac\u90fd\u5e02\u55b6)"},{"areaCode":"16","arraivalTime":"1824","arrivalTime":"1824","code":"25607","departureTime":"1824","getOff":"False","getOn":"False","kenCode":"26","name":"\u70cf\u4e38\u5fa1\u6c60"},{"areaCode":"16","arraivalTime":"1825","arrivalTime":"1825","code":"25681","departureTime":"1826","getOff":"False","getOn":"False","kenCode":"26","name":"\u56db\u6761(\u4eac\u90fd\u5e02\u55b6)"},{"areaCode":"16","arraivalTime":"1827","arrivalTime":"1827","code":"25670","departureTime":"1828","getOff":"False","getOn":"False","kenCode":"26","name":"\u4e94\u6761(\u4eac\u90fd\u5e02\u55b6)"},{"areaCode":"16","arraivalTime":"1829","arrivalTime":"1829","code":"25647","departureTime":"1830","getOff":"False","getOn":"False","kenCode":"26","name":"\u4eac\u90fd"},{"areaCode":"16","arraivalTime":"1831","arrivalTime":"1831","code":"25651","departureTime":"1831","getOff":"False","getOn":"False","kenCode":"26","name":"\u4e5d\u6761(\u4eac\u90fd\u5e9c)"},{"areaCode":"16","arraivalTime":"1832","arrivalTime":"1832","code":"25694","departureTime":"1833","getOff":"False","getOn":"False","kenCode":"26","name":"\u5341\u6761(\u4eac\u90fd\u5e02\u55b6)"},{"areaCode":"16","arraivalTime":"1834","arrivalTime":"1834","code":"25650","departureTime":"1835","getOff":"False","getOn":"False","kenCode":"26","name":"\u304f\u3044\u306a\u6a4b"},{"areaCode":"16","arraivalTime":"1836","arrivalTime":"1836","code":"25701","departureTime":"1838","getOff":"False","getOn":"False","kenCode":"26","name":"\u7af9\u7530(\u4eac\u90fd\u5e9c)"},{"areaCode":"16","arraivalTime":"1840","arrivalTime":"1840","code":"25752","departureTime":"1840","getOff":"False","getOn":"False","kenCode":"26","name":"\u4f0f\u898b(\u4eac\u90fd\u5e9c)"},{"areaCode":"16","arraivalTime":"1842","arrivalTime":"1842","code":"25648","departureTime":"1842","getOff":"False","getOn":"False","kenCode":"26","name":"\u8fd1\u9244\u4e39\u6ce2\u6a4b"},{"areaCode":"16","arraivalTime":"1843","arrivalTime":"1843","code":"25780","departureTime":"1844","getOff":"False","getOn":"False","kenCode":"26","name":"\u6843\u5c71\u5fa1\u9675\u524d"},{"areaCode":"16","arraivalTime":"1847","arrivalTime":"1847","code":"25776","departureTime":"1847","getOff":"False","getOn":"False","kenCode":"26","name":"\u5411\u5cf6"},{"areaCode":"16","arraivalTime":"1850","arrivalTime":"1850","code":"25618","departureTime":"1851","getOff":"False","getOn":"False","kenCode":"26","name":"\u5c0f\u5009(\u4eac\u90fd\u5e9c)"},{"areaCode":"16","arraivalTime":"1852","arrivalTime":"1852","code":"25594","departureTime":"1852","getOff":"False","getOn":"False","kenCode":"26","name":"\u4f0a\u52e2\u7530"},{"areaCode":"16","arraivalTime":"1854","arrivalTime":"1854","code":"25614","departureTime":"1859","getOff":"False","getOn":"False","kenCode":"26","name":"\u5927\u4e45\u4fdd(\u4eac\u90fd\u5e9c)"},{"areaCode":"16","arraivalTime":"1900","arrivalTime":"1900","code":"25653","departureTime":"1901","getOff":"False","getOn":"False","kenCode":"26","name":"\u4e45\u6d25\u5ddd"},{"areaCode":"16","arraivalTime":"1902","arrivalTime":"1902","code":"25716","departureTime":"1902","getOff":"False","getOn":"False","kenCode":"26","name":"\u5bfa\u7530(\u4eac\u90fd\u5e9c)"},{"areaCode":"16","arraivalTime":"1904","arrivalTime":"1904","code":"25722","departureTime":"1904","getOff":"False","getOn":"False","kenCode":"26","name":"\u5bcc\u91ce\u8358"},{"areaCode":"16","arraivalTime":"1907","arrivalTime":"1907","code":"25690","getOff":"False","getOn":"False","kenCode":"26","name":"\u65b0\u7530\u8fba"}]}}}}
Output for 5.1.2 - 5.1.6
Fatal error: Call to undefined function json_encode() in /in/MFkap on line 45
Process exited with code 255.
Output for 5.0.0 - 5.1.1
Fatal error: Call to undefined method SimpleXMLElement::getNameSpaces() in /in/MFkap on line 51
Process exited with code 255.
Output for 4.4.5 - 4.4.9
Fatal error: Call to undefined function: simplexml_load_string() in /in/MFkap on line 40
Process exited with code 255.
Output for 4.3.2 - 4.4.4
Fatal error: Call to undefined function: simplexml_load_string() in /in/MFkap on line 40
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Call to undefined function: simplexml_load_string() in /in/MFkap on line 40