PHP将数组转换为xml

/**
 *   将数组转换为xml
 *    @param array $data    要转换的数组
 *   @param bool $root     是否要根节点
 *   @return string         xml字符串
 *    @author Dragondean
 *    @url    http://www.cnblogs.com/dragondean
 */
function arr_to_xml($data, $root = true)
{
    $str = "";
    if ($root) {
        $str .= "<xml>";
    }
    foreach ($data as $key => $val) {
        if (is_array($val)) {
            $child = arr_to_xml($val, false);
            $str .= "<$key>$child</$key>";
        } else {
            $str .= "<$key><![CDATA[$val]]></$key>";
        }
    }
    if ($root) {
        $str .= "</xml>";
    }
    return $str;
}
原文地址:https://www.cnblogs.com/xiondun/p/12931696.html