1

i have a JSon Array, and i would like to add (not edit) values, but when i try to add it, i get a fatal error.

Here is my code:

Error happens on the first $ext_data['order']['lang_iso']=$lang->iso_code;

$ext_order = array(
                    "order" => $order,
                    "order_detail" => getList($order->id),
                    "order_messages" => Message::getMessagesByOrderId($order->id),
                    "customer" => $customer,
                    "address_delivery" => $address_delivery,
                    "address_invoice" => $address_invoice,
                    "iso_codes" => $iso_codes,
                );
        
        
$ext_data = json_encode($ext_order, JSON_PRETTY_PRINT | JSON_PARTIAL_OUTPUT_ON_ERROR);
        
$ext_data['order']['lang_iso']=$lang->iso_code;
$ext_data['order']['carrier_name']=$carrier->name;
$ext_data['order']['ccurrency_iso']=$currency->iso_code;
$ext_data['customer']['lang_iso']=$lang_customer->iso_code;
$ext_data['address_delivery']['country_iso']=Country::getIsoById($address_delivery->id_country);
$ext_data['address_delivery']['state_iso']=$state_delivery->iso_code;
$ext_data['address_invoice']['country_iso']=Country::getIsoById($address_invoice->id_country);
$ext_data['address_invoice']['state_iso']=$state_invoice->iso_code;
1
  • 1
    You have to add to it before encoding, not after. Commented Mar 10, 2022 at 20:18

1 Answer 1

2
   $order = json_decode(json_encode($order), true);   

   $ext_order = array(
                        "order" => $order,
                        "order_detail" => getList($order->id),
                        "order_messages" => Message::getMessagesByOrderId($order->id),
                        "customer" => $customer,
                        "address_delivery" => $address_delivery,
                        "address_invoice" => $address_invoice,
                        "iso_codes" => $iso_codes,
                    );
    
            
    $ext_order['order']['lang_iso']=$lang->iso_code;
    $ext_order['order']['carrier_name']=$carrier->name;
    $ext_order['order']['ccurrency_iso']=$currency->iso_code;
    $ext_order['customer']['lang_iso']=$lang_customer->iso_code;
    $ext_order['address_delivery']['country_iso']=Country::getIsoById($address_delivery->id_country);
    $ext_order['address_delivery']['state_iso']=$state_delivery->iso_code;
    $ext_order['address_invoice']['country_iso']=Country::getIsoById($address_invoice->id_country);
    $ext_order['address_invoice']['state_iso']=$state_invoice->iso_code;
    
    $ext_data = json_encode($ext_order, JSON_PRETTY_PRINT | JSON_PARTIAL_OUTPUT_ON_ERROR); // changed the order.
Sign up to request clarification or add additional context in comments.

3 Comments

I tried like that the first time, but the error i get is: Cannot use object of type Order as array.
your $order variable is object not an array. Please turn it to array to use it. Like this: json_decode(json_encode($order), true); and put it into $ext_order
Or just use object syntax. $ext_order['order']->lang_iso

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.