简体   繁体   中英

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:

$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:

<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

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:

$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'],
                    ],
                ]],
            ]
    ]
];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM