3v4l.org

run code in 300+ PHP versions simultaneously
<?php function toXml(SimpleXMLElement $xml, array $data, $mainKey = null) { foreach ($data as $key => $value) { // if the key is an integer, it needs text with it to actually work. if (is_numeric($key)) { $key = $mainKey ? : "key_$key"; } if (is_array($value)) { $childXml = $xml->addChild($key); toXml($childXml, $value, substr($key, 0, -1)); } else { $xml->addChild($key, $value); } } return $xml; } // Pretty print Xml function formatXml($simpleXMLElement) { $xmlDocument = new DOMDocument('1.0'); $xmlDocument->preserveWhiteSpace = false; $xmlDocument->formatOutput = true; $xmlDocument->loadXML($simpleXMLElement->asXML()); return $xmlDocument->saveXML(); } $arr_albums = array(); $rows1 = array(array('Id' => 4)); $rows3 = array(array('Id' => 2, 'Name' => 'viktor_rood.png'), array('Id' => 5, 'Name' => 'viktor_geel.png'), array('Id' => 7, 'Name' => 'viktor_paars.png'), array('Id' => 8, 'Name' => 'viktor_aqua.png') ); foreach ($rows1 as $row1) { $arr_images = array(); foreach ($rows3 as $row3) { $image_array = array( 'Id' => $row3["Id"], 'Name' => $row3["Name"] ); $arr_images[] = $image_array; } $album_array = array( 'Id' => $row1["Id"], 'Images' => $arr_images ); $arr_albums[] = $album_array; } $xml = toXml(new SimpleXMLElement('<Albums/>'), $arr_albums, 'Album'); print formatXml($xml);
Output for 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.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.28, 8.4.1 - 8.4.14, 8.5.0
<?xml version="1.0"?> <Albums> <Album> <Id>4</Id> <Images> <Image> <Id>2</Id> <Name>viktor_rood.png</Name> </Image> <Image> <Id>5</Id> <Name>viktor_geel.png</Name> </Image> <Image> <Id>7</Id> <Name>viktor_paars.png</Name> </Image> <Image> <Id>8</Id> <Name>viktor_aqua.png</Name> </Image> </Images> </Album> </Albums>
Output for 8.4.15
/bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libm.so.6: version `GLIBC_2.35' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.34' not found (required by /bin/php-8.4.15) /bin/php-8.4.15: /usr/lib/libc.so.6: version `GLIBC_2.38' not found (required by /bin/php-8.4.15)
Process exited with code 1.

preferences:
181.39 ms | 407 KiB | 5 Q