1

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

2
  • 1
    What you're looking for is a json_encoded string. Commented Jul 29, 2021 at 20:12
  • like $body = json_encode('xxxxxx')? Commented Jul 29, 2021 at 20:29

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Lawrence, thank you for the anwer, it really help me, just forgot one thing, the array $data its dynamic, it could be of x elements, How can I get the values ​​of $data if they are 2 or more elements, in the other array of $ payload
np, glad it helped. If you have multiple you simply need to mutate the items in data with the structure you need, then pass it as a whole 3v4l.org/gI3YG
@David please do not move the "goal posts". When you ask a question (actually, before you post the question), be sure that you have done an adequate job of representing your project data when presenting your minimal reproducible example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.