Convert array or object to XML document

WBOY
Release: 2016-07-25 09:08:57
Original
911 people have browsed it
使用方法: return xml_encode($result);
  1. // xml编码
  2. function xml_encode($data, $encoding='utf-8', $root="root") {
  3. $xml = '';
  4. $xml.= '<' . $root . '>';
  5. $xml.= data_to_xml($data);
  6. $xml.= '';
  7. return $xml;
  8. }
  9. function data_to_xml($data) {
  10. if (is_object($data)) {
  11. $data = get_object_vars($data);
  12. }
  13. $xml = '';
  14. foreach ($data as $key => $val) {
  15. is_numeric($key) && $key = "item id="$key"";
  16. $xml.="<$key>";
  17. $xml.= ( is_array($val) || is_object($val)) ? data_to_xml($val) : $val;
  18. list($key, ) = explode(' ', $key);
  19. $xml.="";
  20. }
  21. return $xml;
  22. }
复制代码


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template