3v4l.org

run code in 300+ PHP versions simultaneously
<?php function array_to_xml($data, &$xml_data) { foreach ($data as $key => $value) { if (is_array($value)) { if (is_numeric($key)){ $key = 'item'.$key; //dealing with <0/>..<n/> issues } $subnode = $xml_data->addChild($key); array_to_xml($value, $subnode); } else { $xml_data->addChild("$key",htmlspecialchars("$value")); } } } $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 = new SimpleXMLElement('<root/>'); array_to_xml($decodedResult, $xmlElement); print_r($xmlElement->xpath('//albumcover/url'));
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 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
Fatal error: Uncaught Error: Call to a member function addChild() on null in /in/44k13:12 Stack trace: #0 /in/44k13(38): array_to_xml(Array, NULL) #1 {main} thrown in /in/44k13 on line 12
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Fatal error: Call to a member function addChild() on null in /in/44k13 on line 12
Process exited with code 255.
Output for 5.5.0 - 5.5.38
Fatal error: Call to a member function addChild() on a non-object in /in/44k13 on line 12
Process exited with code 255.

preferences:
243.14 ms | 401 KiB | 326 Q