php-数组到xml并将数据附加到xml文件
我已经用下面的函数生成了一个xml文件php-数组到xml并将数据附加到xml文件,php,arrays,xml,Php,Arrays,Xml,我已经用下面的函数生成了一个xml文件 $orderInfo = Array ( [order_number] => MI100000038 [customer_id] => 645 [customer_email] => test@yopmail.com [protect_code] => xxxx33639 [total_qty_ordered] => 4.0000 [created_at]
$orderInfo = Array
(
[order_number] => MI100000038
[customer_id] => 645
[customer_email] => test@yopmail.com
[protect_code] => xxxx33639
[total_qty_ordered] => 4.0000
[created_at] => 2016-03-25 20:01:05
[billing_address] => Array
(
[street] => Array
(
[0] => xxx
)
[city] => xx
[region] => xx
[postcode] => 848151
[country_id] => x
[telephone] => 8745165
)
[items] => Array
(
[1] => Array
(
[name] => fgfgf
[sku] => 796
[qty_ordered] => 3.0000
[price] => 81.2000
[product_type] => simple
)
)
) ;
$xml_order_info = new SimpleXMLElement('<?xml version=\'1.0\'?><order_info></order_info>");
$this->array_to_xml($orderInfo,$xml_order_info);
function array_to_xml($array, &$xml_order_info) {
foreach($array as $key => $value) {
if(is_array($value)) {
if(!is_numeric($key)){
$subnode = $xml_order_info->addChild("$key");
$this->array_to_xml($value, $subnode);
}else{
$subnode = $xml_order_info->addChild("item$key");
$this->array_to_xml($value, $subnode);
}
}else {
$xml_order_info->addChild("$key",htmlspecialchars("$value"));
}
}
}
$xml_file = $xml_order_info->asXML('order.xml');
$orderInfo=Array
(
[订单号]=>MI10000038
[客户id]=>645
[客户电子邮件]=>test@yopmail.com
[保护代码]=>XX33639
[总订购数量]=>4.0000
[创建时间]=>2016-03-25 20:01:05
[计费地址]=>阵列
(
[街道]=>阵列
(
[0]=>xxx
)
[城市]=>xx
[地区]=>xx
[邮政编码]=>848151
[国家/地区id]=>x
[电话]=>8745165
)
[项目]=>阵列
(
[1] =>阵列
(
[名称]=>FGF
[sku]=>796
[订购数量]=>3.0000
[价格]=>81.2000
[产品类型]=>简单
)
)
) ;
$xml\u order\u info=新的SimpleXMLElement(“”);
$this->array\u to\u xml($orderInfo,$xml\u order\u info);
函数数组到xml($array,&$xml\u order\u info){
foreach($key=>$value的数组){
if(是_数组($value)){
如果(!是数字($key)){
$subnode=$xml\u order\u info->addChild($key”);
$this->array_to_xml($value,$subnode);
}否则{
$subnode=$xml\u order\u info->addChild(“项目$key”);
$this->array_to_xml($value,$subnode);
}
}否则{
$xml\u order\u info->addChild($key),htmlspecialchars($value));
}
}
}
$xml\u file=$xml\u order\u info->asXML('order.xml');
现在,我想再次使用此过程并将数据附加到exiting order.xml文件中。我使用以下代码:
if (file_exists($xml_order_info_file)) {
echo '<br/>in';
$xml = simplexml_load_string($xml_order_info_file);
if(文件存在($xml\u订单\u信息\u文件)){
回音'
in';
$xml=simplexml\u load\u string($xml\u order\u info\u file);
但它对我不起作用。不希望将文件名作为参数,而只希望使用格式良好的XML字符串:
获取格式良好的XML字符串并将其作为对象返回
因此,最好使用:
if(文件存在($xml\u订单\u信息\u文件)){
回音'
in';
$xml=simplexml\u load\u文件($xml\u order\u info\u文件);
if (file_exists($xml_order_info_file)) {
echo '<br/>in';
$xml = simplexml_load_file($xml_order_info_file);