• 数组和xml的互相转换的封装函数


    /**
    * 数组转换为xml
    *
    * @param array $arr
    * @param int $level
    * @return string
    */
    public static function arrayToXml($arr, $level = 0) {
      $s = $level == 0 ? '<?xml version="1.0" encoding="UTF-8"?>' : '';
      foreach ( $arr as $key => $value ) {
        if (is_array ( $value )) {
          $s .= "<" . $key . ">" . ArrayXML::arrayToXml ( $value, 1 ) . "</" . $key . ">";
        } else {
          $s .= "<" . $key . ">" . $value . "</" . $key . ">";
        }
      }
      return $s;
    }

    /**
    * xml转为数组
    *
    * @param string $xml
    * @param string $version
    * @param string $charset
    * @return array
    */
    public static function xmlToarray($xml, $version = "1.0", $charset = "utf-8") {
      $doc = new DOMDocument ( $version, $charset );
      $doc->loadXML ( $xml );
      $result = domNodeToArray($doc);
      if (isset ( $result ['#document'] )) {
        $result = $result ['#document'];
      }
      return $result;
     }

  • 相关阅读:
    优化总结文章链接
    帧同步、状态同步
    ecs
    AStarPathFinding
    unity 热更方案对比
    C#数据类型
    JavaScript基础
    CSS中margin和padding的区别
    css选择器
    hadoop中使用shell判断HDFS文件是否存在
  • 原文地址:https://www.cnblogs.com/wzjdy/p/7297454.html
Copyright © 2020-2023  润新知