【发布时间】:2017-06-30 20:32:17
【问题描述】:
我正在使用 PHP 进行数组到 XML 的转换。我正在使用以下代码:
function array_to_xml($template_info, &$xml_template_info) {
foreach($template_info as $key => $value) {
if(is_array($value)) {
if(!is_numeric($key)){
$subnode = $xml_template_info->addChild("$key");
if(count($value) >1 && is_array($value)){
$jump = false;
$count = 1;
foreach($value as $k => $v) {
if(is_array($v)){
if($count++ > 1)
$subnode = $xml_template_info->addChild("$key");
array_to_xml($v, $subnode);
$jump = true;
}
}
if($jump) {
goto LE;
}
array_to_xml($value, $subnode);
}
else
array_to_xml($value, $subnode);
}
else{
array_to_xml($value, $xml_template_info);
}
}
else {
$xml_template_info->addChild("$key","$value");
}
LE: ;
}
}
但问题是我无法为具有numerical array 值的sub node 创建数值标准值,
我正在尝试实现以下方法。
Array
(
[DateTimeStamp] => 06/30/2017 08:11:23
[Sender] => Array
(
[SenderID] => TRN
[SenderName] => Transportation Reservation System,Inc.
)
[Recipient] => Array
(
[RecipientID] => DATA1
[RecipientName] =>
)
[Payload] => Array
(
[RateProduct] => Array
(
[0] => Array
(
[RateVendor] => 42074
[RateId] => 13
[RentalLocationID] => 262
[RateCompanyID] => RCR
[TotalPricing] => Array
(
[RentalDays] => 1
[RateCharge] => 127.50
)
[Taxes] => Array
(
[Tax2Amount] => 154.04
[Tax2Rate] => 0.13
)
[0] => Array
(
[DailyExtra] => Array
(
[ExtraCode] => ERF
[ExtraDesc] => ENERGY RECOVERY FEES
)
)
[1] => Array
(
[DailyExtra] => Array
(
[ExtraCode] => ERF
[ExtraDesc] => ENERGY RECOVERY FEES
)
)
)
[1] => Array
(
[RateVendor] => 42074
[RateId] => 13
[RentalLocationID] => 262
[RateCompanyID] => RCR
[TotalPricing] => Array
(
[RentalDays] => 1
[RateCharge] => 127.50
)
[Taxes] => Array
(
[Tax2Amount] => 154.04
[Tax2Rate] => 0.13
)
[0] => Array
(
[DailyExtra] => Array
(
[ExtraCode] => ERF
[ExtraDesc] => ENERGY RECOVERY FEES
)
)
[1] => Array
(
[DailyExtra] => Array
(
[ExtraCode] => ERF
[ExtraDesc] => ENERGY RECOVERY FEES
)
)
)
)
)
)
这个array值需要改成如下格式
<?xml version="1.0" encoding="UTF-8"?>
<PRE>
<TRNXML Version="1.0.0" TimeZone="ET" />
<DateTimeStamp>06/30/2017 04:36:04</DateTimeStamp>
<Sender>
<SenderID>TRN</SenderID>
<SenderName>Transportation Reservation System,Inc.</SenderName>
</Sender>
<Recipient>
<RecipientID>ROUTES</RecipientID>
<RecipientName />
</Recipient>
<Payload>
<RateProduct>
<RateVendor>FCAR</RateVendor>
<RateID />
<RentalLocationID>FCAR</RentalLocationID>
<RateCompanyID>FCAR</RateCompanyID>
<TotalPricing>
<RentalDays>FCAR</RentalDays>
<RateCharge>FCAR</RateCharge>
</TotalPricing>
<Taxes>
<Tax2Amount>FCAR</Tax2Amount>
<Tax2Rate>FCAR</Tax2Rate>
</Taxes>
///this has to come in following format
<DailyExtra>
<ExtraCode>FCAR</ExtraCode>
<ExtraDesc>FCAR</ExtraDesc>
</DailyExtra>
<DailyExtra>
<ExtraCode>FCAR</ExtraCode>
<ExtraDesc>FCAR</ExtraDesc>
</DailyExtra>
</RateProduct>
</Payload>
</PRE>
如何让它在 XML 代码中工作?
【问题讨论】:
标签: php arrays xml codeigniter