Php 将数组转换为简单xml
这是我的阵列:Php 将数组转换为简单xml,php,xml,arrays,simplexml,Php,Xml,Arrays,Simplexml,这是我的阵列: Array ( [0] => Array ( [product_id] => 1 ) [1] => Array ( [tab_id] => 351 ) [2] => Array ( [tab_name] => test1 ) [3] =>
Array
(
[0] => Array
(
[product_id] => 1
)
[1] => Array
(
[tab_id] => 351
)
[2] => Array
(
[tab_name] => test1
)
[3] => Array
(
[region_timezone] => Array
(
[0] => 1
)
[registrationstatus] => Array
(
[0] => 2
)
[eventstatus] => Array
(
[0] => 2
)
[dist_activity] => Array
(
[0] => 5
[1] => 10068
[2] => 10070
)
[dist_region] => Array
(
[0] => 5069
[1] => 5069
[2] => 5069
)
[dist_value] => Array
(
[0] => 55
[1] => 342
[2] => 86
)
[dist_unit] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
[dist_map] => Array
(
[0] => 5
)
[entryfee_currency] => Array
(
[0] => USD
)
[reg_str_dt] => Array
(
[0] => 2013-01-14 20:35:00
)
[reg_end_dt] => Array
(
[0] => 2013-01-14 20:35:00
)
[individual_label] => Array
(
[0] => 19+++
)
[individual_born_from] => Array
(
[0] => 1980-08-21
)
[individual_born_to] => Array
(
[0] => 2010-08-18
)
[individual_sex] => Array
(
[0] => 3
)
[individual_strdt] => Array
(
[0] => 2013-01-14 20:35:00
)
[individual_start] => Array
(
[0] => 2013-01-14 20:35:00
)
[elite_sex] => Array
(
[0] => 1
)
)
)
Array
(
[0] => Array
(
[product_id] => 2
)
[1] => Array
(
[tab_id] => 352
)
[2] => Array
(
[tab_name] => test2
)
[3] => Array
(
[region_timezone] => Array
(
[0] => 1
)
[registrationstatus] => Array
(
[0] => 2
)
[eventstatus] => Array
(
[0] => 2
)
[dist_activity] => Array
(
[0] => 5
[1] => 10069
[2] => 10070
)
[dist_region] => Array
(
[0] => 4457
[1] => 7140
[2] => 5069
)
[dist_value] => Array
(
[0] => 55
[1] => 213
[2] => 86
)
[dist_unit] => Array
(
[0] => 1
[1] => 1
[2] => 1
)
[dist_map] => Array
(
[0] => 5
)
[entryfee_currency] => Array
(
[0] => USD
)
[reg_str_dt] => Array
(
[0] => 2013-02-14 20:39:00
)
[reg_end_dt] => Array
(
[0] => 2013-02-14 20:39:00
)
[individual_label] => Array
(
[0] => 19+++
)
[individual_born_from] => Array
(
[0] => 1980-08-21
)
[individual_born_to] => Array
(
[0] => 2010-08-18
)
[individual_sex] => Array
(
[0] => 3
)
[individual_strdt] => Array
(
[0] => 2013-02-14 20:39:00
)
[individual_start] => Array
(
[0] => 2013-02-14 20:39:00
)
[elite_sex] => Array
(
[0] => 1
)
)
)
如何在PHP中将数组转换为SimpleXML对象?我想将此数组显示为xml(转换为xml)
请帮我解决这个问题…说你的数组是
$data
试试这个
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($data, array ($xml, 'addChild'));
print $xml->asXML();
$xml=新的SimpleXMLElement(“”);
array_walk_recursive($data,array($xml,'addChild'));
打印$xml->asXML();
类ArrayToXML
{
/**
*用于转换为XML文档的主要函数。
*传入一个多维数组,然后重新循环并构建一个XML文档。
*
*@param数组$data
*@param string$rootNodeName-您希望根节点是什么-defaultsto数据。
*@param SimpleXMLElement$xml-只能递归使用
*@returnstringxml
*/
公共静态函数toXml($data,$rootNodeName='data',$xml=null)
{
//关闭兼容性模式,因为如果您不这样做,简单xml会引起不稳定。
if(ini_get('zend.ze1_compatibility_mode')==1)
{
ini_集('zend.ze1_兼容性_模式',0);
}
如果($xml==null)
{
$xml=simplexml\u load\u字符串(“”);
}
//循环遍历传入的数据。
foreach($key=>$value形式的数据)
{
//请不要在我们的xml中使用数字键!
如果(是数字($key))
{
//制作字符串键。。。
$key=“unknownNode_u389;”(字符串)$key;
}
//替换任何非字母数字的内容
$key=preg_replace('/[^a-z]/i',''$key);
//如果重新找到另一个数组,请调用此函数
if(是_数组($value))
{
$node=$xml->addChild($key);
//隐秘的电话。
ArrayToXML::toXml($value,$rootNodeName,$node);
}
其他的
{
//添加单个节点。
$value=htmlentities($value);
$xml->addChild($key,$value);
}
}
//如果需要,可以作为字符串或简单xml对象传回!
返回$xml->asXML();
}
}
类array2xml
{
$output=“”;
函数array2xml($array,$root='root',$element='element')
{
$this->output.=$this->make($array,$root,$element);
}
函数make($array,$root,$element)
{
$xml=“\n”;
foreach($key=>$value的数组){
if(是_数组($value)){
$xml.=$this->make($value、$element、$key);
}否则{
如果(是数字($key)){
$xml.=“{$value}\n”;
}否则{
$xml.=“{$value}\n”;
}
}
}
$xml.=“\n”;
返回$xml;
}
}
echo“从数组转换为xml格式后
”;
echo array2xml($output_array);
class ArrayToXML
{
/**
* The main function for converting to an XML document.
* Pass in a multi dimensional array and this recrusively loops through and builds up an XML document.
*
* @param array $data
* @param string $rootNodeName - what you want the root node to be - defaultsto data.
* @param SimpleXMLElement $xml - should only be used recursively
* @return string XML
*/
public static function toXml($data, $rootNodeName = 'data', $xml=null)
{
// turn off compatibility mode as simple xml throws a wobbly if you don't.
if (ini_get('zend.ze1_compatibility_mode') == 1)
{
ini_set ('zend.ze1_compatibility_mode', 0);
}
if ($xml == null)
{
$xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />");
}
// loop through the data passed in.
foreach($data as $key => $value)
{
// no numeric keys in our xml please!
if (is_numeric($key))
{
// make string key...
$key = "unknownNode_". (string) $key;
}
// replace anything not alpha numeric
$key = preg_replace('/[^a-z]/i', '', $key);
// if there is another array found recrusively call this function
if (is_array($value))
{
$node = $xml->addChild($key);
// recrusive call.
ArrayToXML::toXml($value, $rootNodeName, $node);
}
else
{
// add single node.
$value = htmlentities($value);
$xml->addChild($key,$value);
}
}
// pass back as string. or simple xml object if you want!
return $xml->asXML();
}
}
class array2xml
{
$output = "";
function array2xml($array, $root = 'root', $element = 'element')
{
$this->output .= $this->make($array, $root, $element);
}
function make($array, $root, $element)
{
$xml = "<{$root}>\n";
foreach ($array as $key => $value) {
if (is_array($value)) {
$xml .= $this->make($value, $element, $key);
} else {
if (is_numeric($key)) {
$xml .= "<{$root}>{$value}</{$root}>\n";
} else {
$xml .= "<{$key}>{$value}</{$key}>\n";
}
}
}
$xml .= "</{$root}>\n";
return $xml;
}
}
echo "After convertion from array to xml format <br />";
echo array2xml($output_array);