Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is php 5.2 code which will convert array of any depth to xml document:</p> <pre><code>Array ( ['total_stud']=&gt; 500 [0] =&gt; Array ( [student] =&gt; Array ( [id] =&gt; 1 [name] =&gt; abc [address] =&gt; Array ( [city]=&gt;Pune [zip]=&gt;411006 ) ) ) [1] =&gt; Array ( [student] =&gt; Array ( [id] =&gt; 2 [name] =&gt; xyz [address] =&gt; Array ( [city]=&gt;Mumbai [zip]=&gt;400906 ) ) ) ) </code></pre> <p>generated XML would be as: </p> <pre><code>&lt;?xml version="1.0"?&gt; &lt;student_info&gt; &lt;total_stud&gt;500&lt;/total_stud&gt; &lt;student&gt; &lt;id&gt;1&lt;/id&gt; &lt;name&gt;abc&lt;/name&gt; &lt;address&gt; &lt;city&gt;Pune&lt;/city&gt; &lt;zip&gt;411006&lt;/zip&gt; &lt;/address&gt; &lt;/student&gt; &lt;student&gt; &lt;id&gt;1&lt;/id&gt; &lt;name&gt;abc&lt;/name&gt; &lt;address&gt; &lt;city&gt;Mumbai&lt;/city&gt; &lt;zip&gt;400906&lt;/zip&gt; &lt;/address&gt; &lt;/student&gt; &lt;/student_info&gt; </code></pre> <p>PHP snippet </p> <pre><code>&lt;?php // function defination to convert array to xml function array_to_xml( $data, &amp;$xml_data ) { foreach( $data as $key =&gt; $value ) { if( is_numeric($key) ){ $key = 'item'.$key; //dealing with &lt;0/&gt;..&lt;n/&gt; issues } if( is_array($value) ) { $subnode = $xml_data-&gt;addChild($key); array_to_xml($value, $subnode); } else { $xml_data-&gt;addChild("$key",htmlspecialchars("$value")); } } } // initializing or creating array $data = array('total_stud' =&gt; 500); // creating object of SimpleXMLElement $xml_data = new SimpleXMLElement('&lt;?xml version="1.0"?&gt;&lt;data&gt;&lt;/data&gt;'); // function call to convert array to xml array_to_xml($data,$xml_data); //saving generated xml file; $result = $xml_data-&gt;asXML('/file/path/name.xml'); ?&gt; </code></pre> <p><a href="http://php.net/manual/en/simplexmlelement.asxml.php" rel="noreferrer">Documentation on <code>SimpleXMLElement::asXML</code> used in this snippet</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload