Not so much a tutorial as code snippet, but quite useful in itself.
It is especially helpful when creating XML-based applications, such as AJAX, because you can simply specify something using the standard PHP array model rather than printing/outputting XML.
Heres the code:
function array_to_xml( $input, $level) {
$pre = "\n".str_repeat(" ",($level));
foreach( $input as $key=>$child ) {
if( is_array( $child ) ) {
$output .= $pre."<".$key.">".array_to_xml($child, $level+1).$pre."</".$key.">";
} else {
$output .= $pre."<".$key.">".$child."</".$key.">";
}
}
return $output;
}As you can see it uses recursion to loop through each element.
Technically you could cut it down to about 8 lines, but this is how i lay things out.
Please find attached a file which uses this to output the contents of the parent directory as XML (view source for proper view), and it helps to tell you how to use it as well.
[attachment=
Attached File(s)
-
backend.php (771bytes)
Number of downloads: 4
Help
















