David Solano Rojas
David Solano Rojas

Reputation: 25

concatenate array values ​into string?

I have this array, is stored in a variable called $ data :

Array
(
    [0] => Array
        (
            [Code] => DES001
            [Commennte] => 
            [Quantity] => 1
            [Price] => 135.00
            [Unity] => 1
        )

)

and i want to add to another variable to send to an API

$body = '<request>
                    <method><![CDATA[registra]]></method>
                    <param>
                    <name><![CDATA[pedido]]></name>
                    <value><![CDATA[{"Pedido":
                            {
                             "Nombre":"",
                             "Producto":{
                                "Product":"'.$datos.'"
                            }
                        }
                    }
                    ]]>
        </value>
        </param>
    </params>
</request>';

but when i echo $body the result its like this:

"Product":{
  "Product":"Array"
}

how can I add the string to have an output like this:

"Product":{
     "Product":[
          {"ClaveProducto":"DES001",
          "Cantidad":"1",
          "Precio":"135",
          "Unidad":"PIEZA",
          "Comentario":""
           }]

thanks to all

Upvotes: 1

Views: 129

Answers (1)

Lawrence Cherone
Lawrence Cherone

Reputation: 46620

If you're using XML, it's better to learn and use an XML document API like DOMDocument.

Then you can build out the document like the following, using json_encode to encode the payload array into JSON.

<?php
$data = [
    ['Code' => 'DES001', 'Commennte' => '', 'Quantity' => 1, 'Price' => '135.00', 'Unity' => 1]
];

// expected <value><![CDATA...</value> payload
$payload = [
    'Pedido' => [
        'Nombre' => '',
        'Producto' => [
            'Product' => [
                [
                    'ClaveProducto' => $data[0]['Code'],
                    'Cantidad' => $data[0]['Quantity'],
                    'Precio' => $data[0]['Price'],
                    'Unidad' => 'PIEZA',
                    'Comentario' => ''
                ]
            ]
        ]
    ]
];

// init domdocument
$xml = new DOMDocument('1.0', 'UTF-8');

// optional
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;

// create request wrapper
$request = $xml->createElement('request');

// create method (CDATA), append it to the wrapper
$method = $xml->createElement('method');
$method->appendChild($xml->createCDATASection('registra'));

// create param wrapper
$param = $xml->createElement('param');

// create name (CDATA), append it to the param
$name = $xml->createElement('name');
$name->appendChild($xml->createCDATASection('pedido'));
$param->appendChild($name);

// create value (CDATA), append it to the param
$value = $xml->createElement('value');
$value->appendChild($xml->createCDATASection(json_encode($payload)));
$param->appendChild($value);

// append method to request
$request->appendChild($method);

// append param to request
$request->appendChild($param);

// append request onto doc root
$xml->appendChild($request);

// output
echo $xml->saveXML($xml->documentElement);

The resulting XML will look like:

<request>
  <method><![CDATA[registra]]></method>
  <param>
    <name><![CDATA[pedido]]></name>
    <value><![CDATA[{"Pedido":{"Nombre":"","Producto":{"Product":[{"ClaveProducto":"DES001","Cantidad":"1","Precio":"135","Unidad":"PIEZA","Comentario":""}]}}}]]></value>
  </param>
</request>

Test online: https://3v4l.org/nUSm5

For multiple it would be some thing like: https://3v4l.org/FgrhN

Upvotes: 2

Related Questions