3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php //$xml = simplexml_load_file('data.xml'); $xml = simplexml_load_string('<items> <item> <images> <image>A</image> <image>B</image> <image>C</image> </images> </item> </items>'); // Loop over items in original xml foreach ($xml->item as $item) { if (count($item->images->image) > 1) { // Clone entry $images = clone $item->images; // Remove entry and replace with empty unset($item->images); $item->addChild('images'); for ($i = 0; $i < count($images->image); $i++) { $num = ($i > 0) ? $i : ''; $item->images->addChild('image'.$num, $images->image[$i]); } } } //$xml->asXML('POTO.xml'); echo $xml->asXML();
Output for 5.2.1 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
<?xml version="1.0"?> <items> <item> <images><image>A</image><image1>B</image1><image2>C</image2></images></item> </items>
Output for 5.1.3 - 5.2.0
<?xml version="1.0"?> <items> <item> <images/></item> </items>
Output for 5.1.2
<br /> <b>Fatal error</b>: Call to undefined method SimpleXMLElement::addChild() in <b>/in/m0VoD</b> on line <b>22</b><br />
Process exited with code 255.
Output for 5.0.0 - 5.1.1
<?xml version="1.0"?> <items> <item> <images> <image>A</image> <image>B</image> <image>C</image> </images> </item> </items>
Output for 4.4.2 - 4.4.9
<br /> <b>Parse error</b>: syntax error, unexpected T_VARIABLE in <b>/in/m0VoD</b> on line <b>18</b><br />
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
<br /> <b>Parse error</b>: parse error, unexpected T_VARIABLE in <b>/in/m0VoD</b> on line <b>18</b><br />
Process exited with code 255.
Output for 4.3.2 - 4.3.4
<br /> <b>Parse error</b>: parse error in <b>/in/m0VoD</b> on line <b>18</b><br />
Process exited with code 255.