我有一个这样的数组列表
Array
(
[351] => Array
(
[0] => Array
(
[unit_id] => Array
(
[0] => 10192
[1] => 10192
[2] => 10192
)
[born_from] => Array
(
[0] => 1980-08-21
)
[born_to] => Array
(
[0] => 2010-08-18
)
[start_date] => Array
(
[0] => 2013-02-21 17:29:00
)
[sex] => Array
(
[0] => 3
)
[id] => 351
[product_id] => 1
[name] => test1
)
)
[352] => Array
(
[0] => Array
(
[unit_id] => Array
(
[0] => 10192
[1] => 10192
[2] => 10192
)
[born_from] => Array
(
[0] => 1985-01-01
)
[born_to] => Array
(
[0] => 2000-01-07
)
[sex] => Array
(
[0] => 2
)
[id] => 352
[product_id] => 2
[name] => Test2
)
)
)
现在将此数组转换为 xml 我有此代码
$product_info = array($myPHPArray);
// creating object of SimpleXMLElement
$xml_product_info = new SimpleXMLElement("<?xml version=\"1.0\"?><product_info></product_info>");
// function call to convert array to xml
array_to_xml($product_info,$xml_product_info);
//saving generated xml file
$xml_product_info->asXML('C:/product.xml');
// function defination to convert array to xml
function array_to_xml($product_info, &$xml_product_info) {
foreach($product_info as $key => $value) {
if(is_array($value)) {
if(!is_numeric($key)){
$subnode = $xml_product_info->addChild("$key");
array_to_xml($value, $subnode);
}
else{
array_to_xml($value, $xml_product_info);
}
}
else {
$xml_product_info->addChild("$key","$value");
}
}
}
这会给我这样的输出
<?xml version="1.0"?>
<product_info>
<unit_id>
<0>10192</0>
<1>10192</1>
<2>10192</2>
</unit_id>
<born_from>
<0>1980-08-21</0>
</born_from>
<born_to>
<0>2010-08-18</0>
</born_to>
<individual_strdt>
<0>2013-02-21 17:29:00</0>
</individual_strdt>
<elite_sex>
<0>3</0>
</elite_sex>
<id>351</id>
<product_id>1</product_id>
<name>test1</name>
// here it stars for 352
<unit_id>
<0>10192</0>
<1>10192</1>
<2>10192</2>
</unit_id>
.
.
.
.
</product_info>
这里
1)我不能确定特定元素的值是351
它352
应该喜欢
<?xml version="1.0"?>
<product_info>
<351>
//data of 351
</351>
<352>
//data of 352
</352>
</product_info>
2)当一个特定的子数组被转换为 xml 时,它会存储数据<0>1092</0>
,这会增加我的最终文件的大小
3)我可以使用所有这些子数组的值作为属性吗?
4)任何其他巧妙的方式来安排这些数据
我是 xml 新手,所以欢迎任何新想法