Posted By

iTony on 03/25/08


Tagged

php array xml


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

pagetoscreen
romanos
ishkur
dimispaceman


Array contents to XML format


 / Published in: PHP
 

URL: http://davidwalsh.name/watch-post-save-php-post-data-xml

  1. /* print the contents of a url */
  2. function print_r_xml($arr,$wrapper = 'data',$cycle = 1)
  3. {
  4. //useful vars
  5. $new_line = "n";
  6.  
  7. //start building content
  8. if($cycle == 1) { $output = '<?xml version="1.0" encoding="UTF-8">'.$new_line; }
  9. $output.= tabify($cycle - 1).'<'.$wrapper.'>'.$new_line;
  10. foreach($arr as $key => $val)
  11. {
  12. if(!is_array($val))
  13. {
  14. $output.= tabify($cycle).'<'.htmlspecialchars($key).'>'.$val.'</'.htmlspecialchars($key).'>'.$new_line;
  15. }
  16. else
  17. {
  18. $output.= print_r_xml($val,$key,$cycle + 1).$new_line;
  19. }
  20. }
  21. $output.= tabify($cycle - 1).'</'.$wrapper.'>';
  22.  
  23. //return the value
  24. return $output;
  25. }
  26.  
  27. /* tabify */
  28. function tabify($num_tabs)
  29. {
  30. for($x = 1; $x <= $num_tabs; $x++) { $return.= "t"; }
  31. return $return;
  32. }

Report this snippet  

You need to login to post a comment.