[英]Convert an array to xml in php
I need some help please.我需要一些帮助。 I want to convert an array php to xml tu used for a web service but i couldn't i'll put the exemple:我想将数组 php 转换为用于 Web 服务的 xml tu,但我不能,我将举个例子:
$parameters = [ 'outputFormat' =>
[
'x' => 0,
'y' => 0
] 'letter' =>
[
'service' =>
[
'productCode' => 'code',
'depositDate' => '2019-11-26',
]
]
'customsDeclarations' =>
[
'includeCustomsDeclarations' => 1,
'contents' =>
[
'article' =>
[
0 =>
[
'description' => 'desc 1',
'quantity' => 1,
'weight' => 1,
'value' => '4',
],
1 =>
[
'description' => 'desc 2',
'quantity' => 1,
'weight' => 1,
'value' => '10',
]
],
'category' =>
[
'value' => int 6,
],
]
] ];
i need to convert this array to xml like this my problem is in "article" node:我需要像这样将此数组转换为 xml 我的问题出在“文章”节点中:
<includeCustomsDeclarations>1</includeCustomsDeclarations>
<contents>
<article>
<description>desc1</description>
<quantity>1</quantity>
<weight>1</weight>
<value>1999</value>
</article>
<article>
<description>desc2</description>
<quantity>1</quantity>
<weight>1</weight>
<value>1200</value>
</article>
I use this function to convert我用这个函数来转换
public function convertArrayToXml(array $soapRequest, $soapRequestXml)
{
foreach ($soapRequest as $key => $value) {
if (is_array($value)) {
if (!is_numeric($key)) {
$subNode = $soapRequestXml->addChild("$key");
$this->convertArrayToXml($value, $subNode);
} else {
$subNode = $soapRequestXml->addChild("item$key");
$this->convertArrayToXml($value, $subNode);
}
} else {
$soapRequestXml->addChild("$key", htmlspecialchars("$value"));
}
}
}
It's a common problem to try and add multiple elements in an array underneath a keyed element.尝试在键控元素下的数组中添加多个元素是一个常见问题。 Rather than use the simplified - lets add another level in, this code adds it into the parent element directly.而不是使用简化的 - 让我们添加另一个级别,这段代码直接将它添加到父元素中。 On the second iteration, it looks for the parent node to get the name of the element and creates a new sub-node with this name and adds the new array element to the new node.在第二次迭代中,它查找父节点以获取元素的名称并使用该名称创建一个新的子节点并将新的数组元素添加到新节点。
public function convertArrayToXml(array $soapRequest, $soapRequestXml)
{
foreach ($soapRequest as $key => $value) {
if (is_array($value)) {
if (!is_numeric($key)) {
$subNode = $soapRequestXml->addChild("$key");
$this->convertArrayToXml($value, $subNode);
} else {
// After first element
if ( $key > 0 ) {
// Fetch parent node
$parentNode = $soapRequestXml->xpath("..")[0];
// Add in a new node with same name and set as node to add data to
$subNode = $parentNode->addChild($soapRequestXml->getName());
}
else {
// First time, add it directly to existing node
$subNode = $soapRequestXml;
}
$this->convertArrayToXml($value, $subNode);
}
} else {
$soapRequestXml->addChild("$key", htmlspecialchars("$value"));
}
}
}
For that test data, the output is...对于该测试数据,输出是...
<outputFormat>
<x>0</x>
<y>0</y>
</outputFormat>
<letter>
<service>
<productCode>code</productCode>
<depositDate>2019-11-26</depositDate>
</service>
</letter>
<customsDeclarations>
<includeCustomsDeclarations>1</includeCustomsDeclarations>
<contents>
<article>
<description>desc 1</description>
<quantity>1</quantity>
<weight>1</weight>
<value>4</value>
</article>
<article>
<description>desc 2</description>
<quantity>1</quantity>
<weight>1</weight>
<value>10</value>
</article>
<category>
<value>6</value>
</category>
</contents>
</customsDeclarations>
https://github.com/SrDebiasi/php-xml-array-parser https://github.com/SrDebiasi/php-xml-array-parser
I made this one.我做了这个。 The function is on github at function.php You can use multiple keys and attributes, the array needs to follow this structure:该函数位于 github 上的 function.php 您可以使用多个键和属性,数组需要遵循以下结构:
$array = [
[
'key' => 'car',
'fields' =>
[
['key' => 'wheel', 'value' => 1],
['key' => 'wheel', 'value' => 2],
['key' => 'wheel', 'value' => 3],
['key' => 'tire', 'value' => 3],
['key' => 'wire', 'fields' => [
['key' => 'central', 'value' => 1],
['key' => 'pack', 'attributes' =>
['key' => 'HX', 'value' => 'Right'],
['key' => 'HX', 'value' => 'Right'],
['key' => 'HZ', 'value' => 'Left'],
],
]],
]
]
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.