3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php // From: https://www.kerstner.at/en/2011/12/php-array-to-xml-conversion/ function arrayToXml($array, $rootElement = null, $xml = null) { $_xml = $xml; if ($_xml === null) { $_xml = new SimpleXMLElement($rootElement !== null ? $rootElement : '<root/>'); } foreach ($array as $k => $v) { if (is_array($v)) { //nested array arrayToXml($v, $k, $_xml->addChild($k)); } else { $_xml->addChild($k, $v); } } return $_xml; } $requestResult = <<<EOT { "name": "Benjamin Clementine", "mbid_id": "578e5d1c-be21-4158-b147-30aad67f207d", "albums": { "68e65535-f133-408d-93dd-205e9555eaa5": { "albumcover": [ { "id": "172070", "url": "http://assets.fanart.tv/fanart/music/578e5d1c-be21-4158-b147-30aad67f207d/albumcover/at-least-for-now-55b74dfa59411.jpg", "likes": "0" } ] } } } EOT; $decodedResult = json_decode($requestResult, true); $xmlElement = arrayToXml($decodedResult); var_dump($xmlElement->xpath('/root/albums/*/albumcover/*/url[1]')[0][0]);
based on SgW08
Output for 5.5.27 - 5.5.35, 5.6.11 - 7.2.0
object(SimpleXMLElement)#3 (1) { [0]=> string(119) "http://assets.fanart.tv/fanart/music/578e5d1c-be21-4158-b147-30aad67f207d/albumcover/at-least-for-now-55b74dfa59411.jpg" }
Output for 5.4.0 - 5.5.26, 5.6.8 - 5.6.10
object(SimpleXMLElement)#3 (0) { }
Output for 5.3.19 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/rn6Km on line 43
Process exited with code 255.