Convert.class.php 1023 Bytes
<?php
/**
 * 转换工具类
 * 
 * @name Util_Utils_Convert
 * @package util/utils
 * @copyright yoho.inc
 * @version 0.1 (2013-05-07 10:56:55)
 * @author fei.hong <fei.hong@yoho.cn>
 */
class Util_Utils_Convert
{
    /**
     * 将Array转为Xml格式
     * 
     * @param array $data (数组数据)
     * @param string $node (节点名称)
     * @param string $subnode (子节点名称)
     * @return string
     */
    public static function array2xml($data, $node = 'list') 
    { 
        $xml = '';
    	if (is_array($data))
    	{
    	    $first = key($data);
    		foreach ($data as $key => $value)
    		{
    			if (is_numeric($key))
    			{
    				$key = $node;
    			}
    			$xml .= ($key !== $first) ? "\n" : "";
    			$xml .= "<{$key}>" . self::array2xml($value, $node) . "</{$key}>";
    		}
    	}
    	elseif (is_string($data))
    	{
    	    $xml = htmlspecialchars($data, ENT_QUOTES);
    	}
    	else
    	{
    	    $xml = (string) $data;
    	}
    	return $xml;
    } 
                 
}