Advertisement
michaelyuen

Untitled

May 25th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.64 KB | None | 0 0
  1. $query = "SELECT * from table";
  2. $result = $con->query($query);
  3. WHILE($row = $result->fetch_assoc()) {
  4.     $data[] = $row;
  5. }
  6.  
  7. function array_to_xml( $data, &$xml_data ) {
  8.     foreach( $data as $key => $value ) {
  9.             if( is_numeric($key) ){
  10.                     $key = 'item'.$key; //dealing with <0/>..<n/> issues
  11.             }
  12.             if( is_array($value) ) {
  13.                     $subnode = $xml_data->addChild($key);
  14.                     array_to_xml($value, $subnode);
  15.             } else {
  16.                     $xml_data->addChild("$key",htmlspecialchars("$value"));
  17.             }
  18.      }
  19. }
  20.  
  21. $xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');
  22. array_to_xml($array,$xml_data);
  23. $result = $xml_data->asXML('test.xml');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement