3v4l.org

run code in 300+ PHP versions simultaneously
<?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>'); var_dump($xml); // Loop over items in original xml for($i = 0; $i < count($xml->item); $i++) { //$images; if( (count($xml->item[$i]->images->image)) > 1) { $images = $xml->item[$i]->images; var_dump($images); // Remove entry unset($xml->item[$i]->images); $xml->item[$i]->addChild('images'); for($y = 0; $y < $images->image->count(); $y++) { if($y == 0) { $xx = $xml->item[$i]->images->addChild('image', $images->image[$y]); }else { $xml->item[$i]->images->addChild('image' . $y, $images->image[$y]); } } var_dump($images); } } //$xml->asXML('POTO.xml'); echo $xml->asXML();
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
object(SimpleXMLElement)#1 (1) { ["item"]=> object(SimpleXMLElement)#2 (1) { ["images"]=> object(SimpleXMLElement)#3 (1) { ["image"]=> array(3) { [0]=> string(1) "A" [1]=> string(1) "B" [2]=> string(1) "C" } } } } object(SimpleXMLElement)#3 (1) { ["image"]=> array(3) { [0]=> string(1) "A" [1]=> string(1) "B" [2]=> string(1) "C" } } object(SimpleXMLElement)#3 (0) { } <?xml version="1.0"?> <items> <item> <images/></item> </items>
Output for 5.1.3 - 5.1.6, 5.2.0 - 5.2.17
object(SimpleXMLElement)#1 (1) { ["item"]=> object(SimpleXMLElement)#2 (1) { ["images"]=> object(SimpleXMLElement)#3 (1) { ["image"]=> array(3) { [0]=> string(1) "A" [1]=> string(1) "B" [2]=> string(1) "C" } } } } object(SimpleXMLElement)#5 (1) { ["image"]=> array(3) { [0]=> string(1) "A" [1]=> string(1) "B" [2]=> string(1) "C" } } Fatal error: Call to undefined method SimpleXMLElement::count() in /in/0HabJ on line 29
Process exited with code 255.
Output for 5.1.2
object(SimpleXMLElement)#1 (1) { ["item"]=> object(SimpleXMLElement)#2 (1) { ["images"]=> object(SimpleXMLElement)#3 (1) { ["image"]=> array(3) { [0]=> string(1) "A" [1]=> string(1) "B" [2]=> string(1) "C" } } } } object(SimpleXMLElement)#5 (1) { ["image"]=> array(3) { [0]=> string(1) "A" [1]=> string(1) "B" [2]=> string(1) "C" } } Fatal error: Call to undefined method SimpleXMLElement::addChild() in /in/0HabJ on line 27
Process exited with code 255.
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.1
object(SimpleXMLElement)#1 (1) { ["item"]=> object(SimpleXMLElement)#2 (1) { ["images"]=> object(SimpleXMLElement)#3 (1) { ["image"]=> array(3) { [0]=> string(1) "A" [1]=> string(1) "B" [2]=> string(1) "C" } } } } <?xml version="1.0"?> <items> <item> <images> <image>A</image> <image>B</image> <image>C</image> </images> </item> </items>
Output for 4.4.5 - 4.4.9
Fatal error: Call to undefined function: simplexml_load_string() in /in/0HabJ on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.11, 4.4.0 - 4.4.4
Fatal error: Call to undefined function: simplexml_load_string() in /in/0HabJ on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Call to undefined function: simplexml_load_string() in /in/0HabJ on line 3

preferences:
232.95 ms | 401 KiB | 356 Q