I´m writing a php that should convert an array into xml-structure.
My Problem is that I can´t get the value of the array in my xml-structure. Probably just a small mistake but I don´t find it.
Here´s my code:
<?php
$format = strtolower(@$_GET['format']) == 'json' ? 'json' : 'xml'; //xml ist default
if (isset($_GET['insert_nr']) and $_GET['insert_nr']<>"") $insert_nr=$_GET['insert_nr']; else $insert_nr="4051101000";
$code = $insert_nr;
codegenerator($code, $format);
function codegenerator($code, $format)
{
// Here is some code missing that generates the array $final_array[]
$final_array[] = array(
'var1' => $ans1["$dub1"],
'var2' => $ans2,
'var3' => $ans3,
'var4' => $and3["$dub4"]);
$it = new RecursiveIteratorIterator(new RecursiveArrayIterator($final_array));
if($format == 'json') {
header('Content-type: application/json');
echo json_encode(array('return_list'=>$it));
}
else {
header('Content-type: text/xml');
echo '<list>';
foreach($it as $key => $value) {
echo '<',$key,'>';
if(is_array($value)) {
echo '<',$value,'>',htmlentities($value),'</',$value,'>';
foreach($value as $tag => $val) {
echo '<',$tag,'>',htmlentities($val),'</',$tag,'>';
}
}
echo '</',$key,'>';
}
echo '</list>';
}
}
?>
This is what my xml looks like:
<list>
<var1/>
<var2/>
<var3/>
<var4/>
<list>
Why can´t I get the answers of the variables. It should look like this:
<list>
<var1>xyz</var1>
<var2>4</var2>
<var3>34,0</var3>
<var4>abc</var4>
</list>