这是我的阵列:
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)
请帮帮我...
答案 0 :(得分:0)
说你的数组是$data
试试这个
$xml = new SimpleXMLElement('<root/>');
array_walk_recursive($data, array ($xml, 'addChild'));
print $xml->asXML();
答案 1 :(得分:0)
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();
}
}
答案 2 :(得分:0)
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);