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); echo $xmlElement->asXML(); print_r($xmlElement->xpath('//url'));
based on KAoaY
Output for 5.5.27 - 5.5.38, 5.6.11 - 7.2.0
<?xml version="1.0"?> <root><name>Benjamin Clementine</name><mbid_id>578e5d1c-be21-4158-b147-30aad67f207d</mbid_id><albums><68e65535-f133-408d-93dd-205e9555eaa5><albumcover><0><id>172070</id><url>http://assets.fanart.tv/fanart/music/578e5d1c-be21-4158-b147-30aad67f207d/albumcover/at-least-for-now-55b74dfa59411.jpg</url><likes>0</likes></0></albumcover></68e65535-f133-408d-93dd-205e9555eaa5></albums></root> Array ( [0] => SimpleXMLElement Object ( [0] => http://assets.fanart.tv/fanart/music/578e5d1c-be21-4158-b147-30aad67f207d/albumcover/at-least-for-now-55b74dfa59411.jpg ) )
Output for 5.5.0 - 5.5.26, 5.6.0 - 5.6.10
<?xml version="1.0"?> <root><name>Benjamin Clementine</name><mbid_id>578e5d1c-be21-4158-b147-30aad67f207d</mbid_id><albums><68e65535-f133-408d-93dd-205e9555eaa5><albumcover><0><id>172070</id><url>http://assets.fanart.tv/fanart/music/578e5d1c-be21-4158-b147-30aad67f207d/albumcover/at-least-for-now-55b74dfa59411.jpg</url><likes>0</likes></0></albumcover></68e65535-f133-408d-93dd-205e9555eaa5></albums></root> Array ( [0] => SimpleXMLElement Object ( ) )