Home >php教程 >php手册 >数组数据转换为xml格式

数组数据转换为xml格式

WBOY
WBOYOriginal
2016-06-06 19:32:36993browse

数组数据转换为xml格式 数组可以是任意维数组 无 function xmlData($data, $xml, $k=''){$p = 0;$num = 0;foreach ($data as $key=$value){if (is_numeric($key)){continue;}if (is_array($value) !empty($value)){$tmpData = xmlData($value, $xml, (($p ==

数组数据转换为xml格式
数组可以是任意维数组
function xmlData($data, &$xml, $k='')
{
	$p = 0;
	$num = 0;
	foreach ($data as $key=>$value)
	{
		if (is_numeric($key))
		{
			continue;
		}
		if (is_array($value) && !empty($value))
		{
			$tmpData = xmlData($value, $xml, (($p == 0 && $num == 0) ? '' : $key));
			$xml = $p == 0 && $k == '' && $num == 0 ? '<' . $key . '>' : '';
			$xml .= $tmpData . '</' . $key . '>';
			$p++;
		} else {
			if ($num == 0 && $k != '')
				$xml .= '<' . $k . '>';
			$xml .= '<' . $key . '>' . $value . '</' . $key . '>';
			$num++;
		}
	}

	return $xml;
}
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