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 ); return; } if (is_numeric($key)) { $key = $name; } if (is_array($value)) { $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'), 'opcionais' => array( 'opcional' => array( 'oi' ) ), ) ) ); echo $document->saveXML();
Output for 8.3.0 - 8.3.4, 8.3.6
Warning: Trying to access array offset on int in /in/89Igo on line 13 Warning: Undefined variable $xml_data in /in/89Igo on line 30 Fatal error: Uncaught Error: Call to a member function appendChild() on null in /in/89Igo:30 Stack trace: #0 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcional') #1 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcionais') #2 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'Products') #3 /in/89Igo(5): array_to_xml_aux(Array, Object(DOMDocument)) #4 /in/89Igo(35): array_to_xml(Array) #5 {main} thrown in /in/89Igo on line 30
Process exited with code 255.
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/89Igo on line 13 Warning: Undefined variable $xml_data in /in/89Igo on line 30 Fatal error: Uncaught Error: Call to a member function appendChild() on null in /in/89Igo:30 Stack trace: #0 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcional') #1 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcionais') #2 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'Products') #3 /in/89Igo(5): array_to_xml_aux(Array, Object(DOMDocument)) #4 /in/89Igo(35): array_to_xml(Array) #5 {main} thrown in /in/89Igo on line 30
Process exited with code 255.
Output for 8.2.0 - 8.2.18
Warning: Trying to access array offset on value of type int in /in/89Igo on line 13 Warning: Undefined variable $xml_data in /in/89Igo on line 30 Fatal error: Uncaught Error: Call to a member function appendChild() on null in /in/89Igo:30 Stack trace: #0 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcional') #1 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcionais') #2 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'Products') #3 /in/89Igo(5): array_to_xml_aux(Array, Object(DOMDocument)) #4 /in/89Igo(35): array_to_xml(Array) #5 {main} thrown in /in/89Igo on line 30
Process exited with code 255.
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
Warning: Trying to access array offset on value of type int in /in/89Igo on line 13 Warning: Undefined variable $xml_data in /in/89Igo on line 30 Fatal error: Uncaught Error: Call to a member function appendChild() on null in /in/89Igo:30 Stack trace: #0 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcional') #1 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcionais') #2 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'Products') #3 /in/89Igo(5): array_to_xml_aux(Array, Object(DOMDocument)) #4 /in/89Igo(38): array_to_xml(Array) #5 {main} thrown in /in/89Igo on line 30
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Notice: Trying to access array offset on value of type int in /in/89Igo on line 13 Notice: Undefined variable: xml_data in /in/89Igo on line 30 Fatal error: Uncaught Error: Call to a member function appendChild() on null in /in/89Igo:30 Stack trace: #0 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcional') #1 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcionais') #2 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'Products') #3 /in/89Igo(5): array_to_xml_aux(Array, Object(DOMDocument)) #4 /in/89Igo(38): array_to_xml(Array) #5 {main} thrown in /in/89Igo on line 30
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Fatal error: Uncaught Error: Call to a member function appendChild() on null in /in/89Igo:30 Stack trace: #0 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcional') #1 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcionais') #2 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'Products') #3 /in/89Igo(5): array_to_xml_aux(Array, Object(DOMDocument)) #4 /in/89Igo(38): array_to_xml(Array) #5 {main} thrown in /in/89Igo on line 30
Process exited with code 255.
Output for 7.3.16 - 7.3.31
Notice: Undefined variable: xml_data in /in/89Igo on line 30 Fatal error: Uncaught Error: Call to a member function appendChild() on null in /in/89Igo:30 Stack trace: #0 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcional') #1 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcionais') #2 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'Products') #3 /in/89Igo(5): array_to_xml_aux(Array, Object(DOMDocument)) #4 /in/89Igo(38): array_to_xml(Array) #5 {main} thrown in /in/89Igo on line 30
Process exited with code 255.
Output for 7.1.4 - 7.1.23, 7.2.0 - 7.2.33
Notice: Undefined variable: xml_data in /in/89Igo on line 30 Fatal error: Uncaught Error: Call to a member function appendChild() on null in /in/89Igo:30 Stack trace: #0 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcional') #1 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcionais') #2 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'Products') #3 /in/89Igo(5): array_to_xml_aux(Array, Object(DOMDocument)) #4 /in/89Igo(37): array_to_xml(Array) #5 {main} thrown in /in/89Igo on line 30
Process exited with code 255.
Output for 7.1.0 - 7.1.3
Notice: Undefined variable: xml_data in /in/89Igo on line 30 Fatal error: Uncaught Error: Call to a member function appendChild() on null in /in/89Igo:30 Stack trace: #0 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcional') #1 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'opcionais') #2 /in/89Igo(28): array_to_xml_aux(Array, Object(DOMElement), 'Products') #3 /in/89Igo(5): array_to_xml_aux(Array, Object(DOMDocument)) #4 /in/89Igo(46): array_to_xml(Array) #5 {main} thrown in /in/89Igo on line 30
Process exited with code 255.
Output for 5.6.38
Notice: Undefined variable: xml_data in /in/89Igo on line 30 Fatal error: Call to a member function appendChild() on null in /in/89Igo on line 30
Process exited with code 255.

preferences:
154.66 ms | 401 KiB | 173 Q