如何在php中回显json对象属性?

时间:2017-04-20 20:05:06

标签: php arrays json object

JSON数据

"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
     }]

在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>";}
}

但我收到以下错误消息,

注意:尝试在....中获取非对象的属性

如何在里面回复数据&#34; billing_details&#34;数组下的对象&#34;命令&#34; ?

2 个答案:

答案 0 :(得分:1)

试试这个

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

echo $json_output;

答案 1 :(得分:1)

试试这个:

template<typename T, typename U>
T safer_cast(const U& from) {
    T to;
    memcpy(&to, &from, (sizeof(T) > sizeof(U) ? sizeof(U) : sizeof(T)));
    return to;
}