title: Convert JSON to XML using PHP
body: <p>My Task is to convert the JSON data in to XML. Right now, My array_to_xml function converts the data from the array into:</p>
  
  <blockquote>
    <p><code>&lt;0&gt;Maserati&lt;/0&gt;&lt;1&gt;BMW&lt;/1&gt;&lt;2&gt;Mercedes/2&gt;&lt;3&gt;Ford&lt;/3&gt;&lt;4&gt;Chrysler&lt;/4&gt;&lt;5&gt;Acura&lt;/5&gt;&lt;6&gt;Honda&lt;/6&gt;&lt;0&gt;1&lt;/0&gt;&lt;1&gt;2&lt;/1&gt;&lt;2&gt;1&lt;/2&gt;&lt;3&gt;0&lt;/3&gt;&lt;4&gt;4&lt;/4&gt;&lt;5&gt;0&lt;/5&gt;&lt;6&gt;0&lt;/6&gt;&lt;0&gt;1&lt;/0&gt;&lt;1&gt;2&lt;/1&gt;&lt;2&gt;1&lt;/2&gt;&lt;3&gt;0&lt;/3&gt;&lt;4&gt;4&lt;/4&gt;&lt;5&gt;0&lt;/5&gt;&lt;6&gt;0&lt;/6&gt;</code></p>
  </blockquote>
  
  <p>I would like to have the XML in the following format:</p>
  
  <pre><code>  &lt;Maserati&gt; 1 &lt;/Maserati&gt;
    &lt;BMW&gt; 2 &lt;/BMW&gt;
    ...
  </code></pre>
  
  <p>Please take a look at the PHP below and let me know the changes to be made.
  Thanks in advance</p>
  
  <pre><code>$inpData =  $_POST['data'];
  // initializing array
  $inp_info =  $inpData;
  // creating object of SimpleXMLElement
  $xml_inp_info = new SimpleXMLElement("&lt;?xml version=\"1.0\" encoding=\"UTF-8\"   standalone=\"yes\"?&gt;&lt;demandSignals&gt;\n&lt;/demandSignals&gt;\n");
  // function call to convert array to xml
  array_to_xml($inp_info,$xml_inp_info);
  //saving generated xml file
  $xml_inp_info-&gt;asXML(dirname(__FILE__)."/demand.xml") ;
  // function definition to convert array to xml
  function array_to_xml($inp_info, &amp;$xml_inp_info) {
          foreach($inp_info as $key =&gt; $value) {
  
              if(is_array($value)) {
                  if(!is_numeric($key)){
                      $subnode = $xml_inp_info-&gt;addChild("$key"); 
                      if(count($value) &gt;1 &amp;&amp; is_array($value)){
                          $jump = false;
                          $count = 1;
                          foreach($value as $k =&gt; $v) {
                              if(is_array($v)){
                                  if($count++ &gt; 1)
                                      $subnode = $xml_inp_info-&gt;addChild("$key");
  
                                  array_to_xml($v, "$subnode");
                                  $jump = true;
                              }
                          }
                          if($jump) {
                              goto LE;
                          }
                          array_to_xml($value, "\n$subnode");
                          }
                      else
                          array_to_xml($value, $subnode);
                  }
                  else{
                      array_to_xml($value, $xml_inp_info);
                  }
              }
              else {
  
                  $xml_inp_info-&gt;addChild("$key","$value");
              }
  
              LE: ;
          }
      }
  </code></pre>