Home  >  Article  >  Backend Development  >  How to convert php array to xml format

How to convert php array to xml format

PHPz
PHPzOriginal
2023-04-23 17:49:28936browse

In PHP, conversion between arrays and XML data is very common. This article will introduce you to several ways to convert PHP arrays to XML format.

  1. Manually construct XML

First, we can manually construct XML for conversion. Specifically, we can use PHP's DOM extension to create a DOM object according to the structure of the XML document, and then insert the array data into the corresponding node. Here is a simple example:

<?php
$my_array = array(
  &#39;person&#39; => array(
    'name' => 'Jack',
    'age' => 30,
    'email' => 'jack@example.com',
  ),
);

$doc = new DOMDocument();
$root = $doc->createElement('root');
$doc->appendChild($root);

function array_to_xml($data, &$xml) {
  foreach($data as $key => $value) {
    if(is_array($value)) {
      $subnode = $xml->createElement($key);
      $xml->appendChild($subnode);
      array_to_xml($value, $subnode);
    } else {
      $xml->appendChild($xml->createElement($key,$value));
    }
  }
}

array_to_xml($my_array, $root);
echo $doc->saveXML();
?>
  1. Using SimpleXML

PHP also provides an easier way to convert associative arrays directly to XML using the SimpleXML extension. Here is sample code:

<?php
$my_array = array(
  &#39;person&#39; => array(
    'name' => 'Jack',
    'age' => 30,
    'email' => 'jack@example.com',
  ),
);

$xml = new SimpleXMLElement('<root/>');

function array_to_xml($data, &$xml) {
  foreach($data as $key => $value) {
    if(is_array($value)) {
      array_to_xml($value, $xml->addChild($key));
    } else {
      $xml->addChild("$key",htmlspecialchars("$value"));
    }
  }
}

array_to_xml($my_array, $xml);
echo $xml->asXML();
?>
  1. Using XMLWriter

An alternative is to use the XMLWriter extension. XMLWriter can help us control the creation process of XML files more finely. Here is the sample code:

<?php
$my_array = array(
  &#39;person&#39; => array(
    'name' => 'Jack',
    'age' => 30,
    'email' => 'jack@example.com',
  ),
);

$xml = new XMLWriter();
$xml->openMemory();
$xml->startDocument('1.0','UTF-8');
$xml->startElement('root');

function array_to_xml($data, &$xml) {
  foreach($data as $key => $value) {
    if(is_array($value)) {
      $xml->startElement($key);
      array_to_xml($value, $xml);
      $xml->endElement();
    } else {
      $xml->writeElement($key, $value);
    }
  }
}

array_to_xml($my_array, $xml);
$xml->endElement();
echo $xml->outputMemory(TRUE);
?>

Summary

The above are three ways to convert PHP arrays to XML. Manually constructing XML is the most tedious method, but it is also the most flexible and allows fine-grained control over the XML creation process. SimpleXML is a simpler option that converts associative arrays directly into XML format. XMLWriter can help us control the creation process of XML files more finely. Depending on actual needs, you can choose one or more of these methods to convert arrays into XML formats.

The above is the detailed content of How to convert php array to xml format. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn