0

JSON data

"orders":[  
     "billing_details":{  
        "company":"Test Company",
        "firstname":"Munadil",
        "postcode":"5000",
        "street":"Dhaka, Bangladesh",
        "email":"munadil98@gmail.com",
        "lastname":"Fahad",
        "ph_number":"880191111111",
        "city":"Dhaka",
        "state":"Mirpur",
        "country_code":"BN",
        "user_id":16003511,
        "salutation":null
     }]

In PHP

$json_output = json_decode($response);
foreach ( $json_output->orders as $orders ){
foreach ($orders->billing_details as $billing_details) {echo "<b>Name:</b><br>".$billing_details->firstname." ".$billing_details->lastname."<br>";}
}

But I am getting below error message,

Notice: Trying to get property of non-object in ....

How can I echo data inside "billing_details" object under array "orders" ?

Munadil Fahad
  • 33
  • 1
  • 9

2 Answers2

2

Try this:

$json_output = json_decode($response);

foreach ($json_output['orders'] as $billing_details) {
echo "<b>Name:</b><br>$billing_details['firstname'] $billing_details['lastname']<br>";}
Omi
  • 3,954
  • 5
  • 21
  • 41
1

Try this

$json_output = json_decode($response['orders']);

echo $json_output;

Muhammad Ahsan
  • 297
  • 2
  • 15
  • How do you echo an array? Btw, `$response` is a string at this point so `$response['orders']` will fail. – M. Eriksson Apr 20 '17 at 20:20
  • Problem solved. Actually, I found that some "orders" array don't have "billing_details" object. This was the reason of returning error. For the "orders" array which have "billing_details" object found ok. So, I get rid of showing error message by if else condition. Thank you all for your support. – Munadil Fahad May 01 '17 at 10:42
  • Your problem solved.Thats great!! – Muhammad Ahsan May 01 '17 at 13:55