如何确保复杂的PHP
变量中存在密钥?
以下是JSON
的<{1}}输出:
Google API
有时候会改变这个:
{
"destination_addresses" : [
"India"
],
"origin_addresses" : [
"India"
],
"rows" : [
{
"elements" : [
{
"distance" : {
"text" : "86 m",
"value" : 86
},
"duration" : {
"text" : "1 min",
"value" : 24
},
"status" : "OK"
}
]
}
],
"status" : "OK"
}
我已使用
将此{
"destination_addresses" : [ "20.348326,85.8160893" ],
"origin_addresses" : [ "20.3487083,-85.8157674" ],
"rows" : [
{
"elements" : [
{
"status" : "ZERO_RESULTS"
}
]
}
],
"status" : "OK"
}
转换为JSON
变量
PHP
现在,我如何确保只在输出中存在关键距离,例如:
$response_a = json_decode($response, true);
如果回应1;否则
$dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
答案 0 :(得分:1)
您可以使用isset(...)例如:
if (isset($response_a['rows'][0]['elements'][0]['distance']['text'])) {
$dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
} else {
$dist =1;
} ;