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 git.master, git.master_jit, rfc.property-hooks
<?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>

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
160.44 ms | 406 KiB | 5 Q