-1

I cannot access the contents within the orders array. I am currently doing this to no avail, and am wondering what I am doing wrong:

the $json object is a json response from a rest api.

$orderData = $json['orders'];
  foreach($orderData['orders'] as $order){
}

{  
   "errors":[  

   ],
   "orders":[  
      {  
         "note":"",
         "estimated_shipping_fee":"0.00",
         "payment_method":"PAY_CYBERSOURCE",
         "escrow_amount":"12.95",
         "message_to_seller":"",
         "shipping_carrier":"Singpost - Normal Mail",
         "currency":"SGD",
         "create_time":1532064559,
         "pay_time":1532064618,
         "recipient_address":{  
            "town":"",
            "city":"",
            "name":"Teo",
            "district":"",
            "country":"SG",
            "zipcode":"41253",
            "full_address":"In some street somewhjere",
            "phone":"23154991",
            "state":""
         },
         "days_to_ship":3,
         "tracking_no":"",
         "order_status":"SHIPPED",
         "note_update_time":0,
         "update_time":1532082525,
         "goods_to_declare":false,
         "total_amount":"12.95",
         "service_code":"",
         "country":"SG",
         "actual_shipping_cost":"",
         "cod":false,
         "items":[  
            {  
               "weight":1.0,
               "item_name":"ABC",
               "is_wholesale":false,
               "item_sku":"5123433123",
               "variation_discounted_price":"12.95",
               "variation_id":0,
               "variation_name":"",
               "item_id":1126534500,
               "variation_quantity_purchased":1,
               "variation_sku":"",
               "variation_original_price":"12.95"
            }
         ],
         "ordersn":"3589290984539"
      }
   ]
}

Trying to access the variables directly, by say json['orders'][0]['payment_method'] is not working.

Tice
  • 103
  • 1
  • 10
  • 1
    You used `json_decode`? Are you getting an error? – ficuscr Jul 20 '18 at 18:32
  • 1
    When you use `json_decode` (assuming, of course, that you did, in fact, use `json_decode`), the option to convert the result into an associative array is set, by default, to `FALSE`. So if you just did something like this: `$json = json_decode($theResponse);`, you won't be able to access the result with an array syntax like `$json['orders'][0]['payment_method']` – Adam Nathaniel Davis Jul 20 '18 at 18:37
  • $responseData = json_decode($server_output, TRUE); I specify this parameter to true :( – Tice Jul 20 '18 at 18:39

2 Answers2

0

convert it to array from json using json_decode(); then you can easily access it and if you want to access using jquery then just convert it object using jQuery.parseJSON();

0

just use json_decode for retrieving the object and then $obj->note or you can turn it into an array: $array = get_object_vars($obj); like in this answer .

Deadpool
  • 735
  • 8
  • 15