3v4l.org

run code in 300+ PHP versions simultaneously
<?php function array_to_xml(array $data) { $document = new DOMDocument(); array_to_xml_aux($data, $document); $document->formatOutput = true; return $document; } function array_to_xml_aux(array $data, DOMNode $parent, $name = null) { foreach ($data as $key => $value) { if ($key[0] == '@') { $parent->setAttribute( substr($key, 1), $value ); continue; } if (is_numeric($key)) { $key = $name; } if (is_array($value)) { $areAllInt = true; foreach(array_keys($value) as $k) { if (!is_int($k)) { $areAllInt = false; break; } } if ($areAllInt) { array_to_xml_aux($value, $parent, $key); } else { $subnode = new DOMElement($key); $parent->appendChild($subnode); array_to_xml_aux($value, $subnode, $key); } } else { $xml_data->appendChild(new DOMElement($key, $value)); } } } $document = array_to_xml( array( 'Products' => array( 'Product' => array('@name' => 'TR-501'), 'Descricao' => array('@text' => '55.180.198 / 46789771'), 'Detalhes' => array( 'Variacao' => array( array( '@modelo' => 'Palio', '@ano' => '2006', '@motor' => '1.0 FIRE 8V (FLEX)', ), array( '@modelo' => 'Palio2', '@ano' => '2007', '@motor' => '1.0 FIRE 9V (FLEX)', ), ) ), ) ) ); echo $document->saveXML();
Output for 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
Warning: Trying to access array offset on int in /in/BquYA on line 13 Warning: Trying to access array offset on int in /in/BquYA on line 13 <?xml version="1.0"?> <Products> <Product name="TR-501"/> <Descricao text="55.180.198 / 46789771"/> <Detalhes> <Variacao modelo="Palio" ano="2006" motor="1.0 FIRE 8V (FLEX)"/> <Variacao modelo="Palio2" ano="2007" motor="1.0 FIRE 9V (FLEX)"/> </Detalhes> </Products>
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Warning: Trying to access array offset on int in /in/BquYA on line 13 Warning: Trying to access array offset on int in /in/BquYA on line 13 <?xml version="1.0"?> <Products> <Product name="TR-501"/> <Descricao text="55.180.198 / 46789771"/> <Detalhes> <Variacao modelo="Palio" ano="2006" motor="1.0 FIRE 8V (FLEX)"/> <Variacao modelo="Palio2" ano="2007" motor="1.0 FIRE 9V (FLEX)"/> </Detalhes> </Products>
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18
Warning: Trying to access array offset on value of type int in /in/BquYA on line 13 Warning: Trying to access array offset on value of type int in /in/BquYA on line 13 <?xml version="1.0"?> <Products> <Product name="TR-501"/> <Descricao text="55.180.198 / 46789771"/> <Detalhes> <Variacao modelo="Palio" ano="2006" motor="1.0 FIRE 8V (FLEX)"/> <Variacao modelo="Palio2" ano="2007" motor="1.0 FIRE 9V (FLEX)"/> </Detalhes> </Products>
Output for 7.4.0 - 7.4.33
Notice: Trying to access array offset on value of type int in /in/BquYA on line 13 Notice: Trying to access array offset on value of type int in /in/BquYA on line 13 <?xml version="1.0"?> <Products> <Product name="TR-501"/> <Descricao text="55.180.198 / 46789771"/> <Detalhes> <Variacao modelo="Palio" ano="2006" motor="1.0 FIRE 8V (FLEX)"/> <Variacao modelo="Palio2" ano="2007" motor="1.0 FIRE 9V (FLEX)"/> </Detalhes> </Products>
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33
<?xml version="1.0"?> <Products> <Product name="TR-501"/> <Descricao text="55.180.198 / 46789771"/> <Detalhes> <Variacao modelo="Palio" ano="2006" motor="1.0 FIRE 8V (FLEX)"/> <Variacao modelo="Palio2" ano="2007" motor="1.0 FIRE 9V (FLEX)"/> </Detalhes> </Products>

preferences:
237.91 ms | 403 KiB | 297 Q