How is the better way for extract data this array con multiples objetcs with php

I have the following JSON response and I need to create an XML block for each sku and quantity data of each in the order. I include all the json response where I am pulling the data from.

Array
(
    https://stackoverflow.com/q/73132619 => 17
    [parent_id] => 0
    [status] => on-hold
    [currency] => MXN
    [version] => 6.7.0
    [prices_include_tax] => 
    [date_created] => 2022-07-27T05:02:25
    [date_modified] => 2022-07-27T05:02:26
    [discount_total] => 0.00
    [discount_tax] => 0.00
    [shipping_total] => 0.00
    [shipping_tax] => 0.00
    [cart_tax] => 0.00
    [total] => 3356.00
    [total_tax] => 0.00
    [customer_id] => 1
    [order_key] => wc_order_xakP1gXCne19m
    [billing] => Array
        (
            [first_name] => Nombre
            [last_name] => sjdjjje
            [company] => 
            [address_1] => Pruena
            [address_2] => Prueba
            [city] => hidalgo
            [state] => AG
            [postcode] => 85422
            [country] => MX
            [email] => tuempresaaswe@gmail.com
            [phone] => 5555555555
        )

    [shipping] => Array
        (
            [first_name] => Nombre
            [last_name] => sjdjjje
            [company] => 
            [address_1] => Pruena
            [address_2] => Prueba
            [city] => hidalgo
            [state] => AG
            [postcode] => 85422
            [country] => MX
            [phone] => 
        )

    [payment_method] => bacs
    [payment_method_title] => Direct bank transfer
    [transaction_id] => 
    [customer_ip_address] => 187.190.230.169
    [customer_user_agent] => Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36
    [created_via] => checkout
    [customer_note] => 
    [date_completed] => 
    [date_paid] => 
    [cart_hash] => 2aa27292dcdf8556b9443b1598c3eb3d
    [number] => 17
    [meta_data] => Array
        (
            [0] => Array
                (
                    https://stackoverflow.com/q/73132619 => 149
                    [key] => is_vat_exempt
                    [value] => no
                )

            [1] => Array
                (
                    https://stackoverflow.com/q/73132619 => 153
                    [key] => _new_order_email_sent
                    [value] => true
                )

            [2] => Array
                (
                    https://stackoverflow.com/q/73132619 => 154
                    [key] => _om_revenue_attribution_data
                    [value] => Array
                        (
                            [transaction_id] => 17
                            [value] => 3356.00
                        )

                )

        )

    [line_items] => Array
        (
            [0] => Array
                (
                    https://stackoverflow.com/q/73132619 => 4
                    [name] => Prueba 1
                    [product_id] => 11
                    [variation_id] => 0
                    [quantity] => 1
                    [tax_class] => 
                    [subtotal] => 122.00
                    [subtotal_tax] => 0.00
                    [total] => 122.00
                    [total_tax] => 0.00
                    [taxes] => Array
                        (
                        )

                    [meta_data] => Array
                        (
                            [0] => Array
                                (
                                    https://stackoverflow.com/q/73132619 => 51
                                    [key] => _reduced_stock
                                    [value] => 1
                                    [display_key] => _reduced_stock
                                    [display_value] => 1
                                )

                        )

                    [sku] => HDF-CS65
                    [price] => 122
                    [image] => Array
                        (
                            https://stackoverflow.com/q/73132619 => 
                            [src] => 
                        )

                    [parent_name] => 
                )

            [1] => Array
                (
                    https://stackoverflow.com/q/73132619 => 5
                    [name] => ramzilla
                    [product_id] => 12
                    [variation_id] => 0
                    [quantity] => 1
                    [tax_class] => 
                    [subtotal] => 3234.00
                    [subtotal_tax] => 0.00
                    [total] => 3234.00
                    [total_tax] => 0.00
                    [taxes] => Array
                        (
                        )

                    [meta_data] => Array
                        (
                            [0] => Array
                                (
                                    https://stackoverflow.com/q/73132619 => 52
                                    [key] => _reduced_stock
                                    [value] => 1
                                    [display_key] => _reduced_stock
                                    [display_value] => 1
                                )

                        )

                    [sku] => RAMZ-023
                    [price] => 3234
                    [image] => Array
                        (
                            https://stackoverflow.com/q/73132619 => 
                            [src] => 
                        )

                    [parent_name] => 
                )

        )

    [tax_lines] => Array
        (
        )

    [shipping_lines] => Array
        (
            [0] => Array
                (
                    https://stackoverflow.com/q/73132619 => 6
                    [method_title] => Free shipping
                    [method_id] => free_shipping
                    [instance_id] => 1
                    [total] => 0.00
                    [total_tax] => 0.00
                    [taxes] => Array
                        (
                        )

                    [meta_data] => Array
                        (
                            [0] => Array
                                (
                                    https://stackoverflow.com/q/73132619 => 50
                                    [key] => Items
                                    [value] => Prueba 1 × 1, ramzilla × 1
                                    [display_key] => Items
                                    [display_value] => Prueba 1 × 1, ramzilla × 1
                                )

                        )

                )

        )

    [fee_lines] => Array
        (
        )

    [coupon_lines] => Array
        (
        )

    [refunds] => Array
        (
        )

    [payment_url] => https://lucky.lab-sdma.link/checkout/order-pay/17/?pay_for_order=true&key=wc_order_xakP1gXCne19m
    [is_editable] => 1
    [needs_payment] => 
    [needs_processing] => 1
    [date_created_gmt] => 2022-07-27T05:02:25
    [date_modified_gmt] => 2022-07-27T05:02:26
    [date_completed_gmt] => 
    [date_paid_gmt] => 
    [currency_symbol] => $
    [_links] => Array
        (
            [self] => Array
                (
                    [0] => Array
                        (
                            [href] => https://lucky.lab-sdma.link/wp-json/wc/v3/orders/17
                        )

                )

            [collection] => Array
                (
                    [0] => Array
                        (
                            [href] => https://lucky.lab-sdma.link/wp-json/wc/v3/orders
                        )

                )

            [customer] => Array
                (
                    [0] => Array
                        (
                            [href] => https://lucky.lab-sdma.link/wp-json/wc/v3/customers/1
                        )

                )

        )

)

But I can’t find how to do it since it only brings me the first value. Can you help me?

my code goes something like this

<?
         header('Content-Type: application/json; charset=utf-8');
    
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    // fetch RAW input
   $json = file_get_contents('php://input');

    // decode json
    $object = json_decode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    //$respuestajson = json_encode ($object);

    // expecting valid json
    if (json_last_error() !== JSON_ERROR_NONE) {
        die(header('HTTP/1.0 415 Unsupported Media Type'));
    }

    /**
     * Do something with object, structure will be like:
     * $object->accountId
     * $object->details->items[0]['contactName']
     */
    // dump to file so you can see

$nombre = $object [billing][first_name];
$apellido = $object [billing][last_name];
$direccion = $object [shipping][address_1];
$direccion2 = $object [shipping][address_2];
$ciudad = $object [shipping][city];
$estado = $object [shipping][state];
$cp = $object [shipping][postcode];
$pais = $object [shipping][country];
$OC = $object https://stackoverflow.com/q/73132619;
$SKU = $object [line_items][0][sku];
$cantidad = $object [line_items][0][quantity];
$completa = [$nombre, $apellido, $direccion, $direccion2, $ciudad, $estado, $cp, $pais];
$contador = 0;

$datesku = [];
$datecantidad = [];
$i = 0;



$SKUbloque="<CodigoProducto>".$SKU.'</CodigoProductoAC>
              <Cantidad>'.$cantidad.'</Cantidad>';

file_put_contents('pruebadatos.json', print_r($completa, true));
    file_put_contents('callback.txt', print_r($object, true));
    file_put_contents('respuesta.json', print_r($object, true));

echo $completa;
echo $OC;

    $datos="<data>
    <Usuario>xxxx</Usuario>
    <Contrasena>xxxx</Contrasena>
                    <OC>
                    <NumeroOC>".$OC.'</NumeroOC>
                    <DireccionEntrega>'.$completa.'</DireccionEntrega>
                    <Paqueteria>000</Paqueteria>
                    <!--<CodigoSucursal>GDL</CodigoSucursal>-->
                    <CodigoSucursal>CEDIS</CodigoSucursal>
                    <FormaPago>Credito30</FormaPago>
                    <PedidoParcial>N</PedidoParcial>
                    <Observaciones>Cliente recoge</Observaciones>
                    <Productos>
                        <Producto>
                            <CodigoProductoAC>'.$SKU.'</CodigoProductoAC>
                            <Cantidad>'.$cantidad.'</Cantidad>
                        </Producto>
                    </Productos>
                </OC>
                   
              </data>';
    
    $Ejo = curl_init();
    $url="https://cxxxx";
    curl_setopt($Ejo, CURLOPT_URL, $url);
    curl_setopt($Ejo, CURLOPT_HTTPHEADER, array('Content-Type: application/xml','Accept: application/xml'));
    curl_setopt($Ejo, CURLOPT_POST, 1);
    curl_setopt($Ejo, CURLOPT_POSTFIELDS,$datos);
    curl_setopt($Ejo, CURLOPT_RETURNTRANSFER, true);
    $respuesta  = curl_exec($Ejo);
    
    if($respuesta === false){
        echo 'Curl error: ' . curl_error($Ejo);
        file_put_contents('respuestpedido.txt', print_r($respuesta, true));
    }
    else{
     
      echo "<pre> $respuesta";
      file_put_contents('respuestpedido.txt', print_r($respuesta, true));
      If (unlink('respuesta.json')) {
        echo "El archivo respuesta.json ha sido eliminado";
      } else {
        echo "Bro, se resistiĆ³, no pudimos eliminarlo";
      }
        
    
    
      }
    curl_close($Ejo); 
       
   

}
?>

Leave a Comment