[英]When i apply array_key_exists funciton it doesnt return value from JSON
I am using PHP to get data from JSON and when i test the key in JSON with array_key_exists to make sure it exists it returns nothing. 我正在使用PHP从JSON获取数据,当我使用array_key_exists在JSON中测试密钥以确保它存在时,它不会返回任何内容。 Here is the link to the library: https://api.edmunds.com/api/vehicle/v2/vins/4T1BK1EB6DU056165?&fmt=json&api_key=zzasv56vxwkx67m9cy6apfmq When i try to run it like this i get nothing:
这是图书馆的链接: https : //api.edmunds.com/api/vehicle/v2/vins/4T1BK1EB6DU056165?&fmt=json&api_key=zzasv56vxwkx67m9cy6apfmq当我尝试像这样运行时,我什么也得不到:
if(array_key_exists('years[0]->year', $jfo)) {
$mds = $jfo->years[0]->year;
}else{
$mds="mds not found";
}
And this way it works: 这种方式有效:
$mds = $jfo->years[0]->year;
Assuming that you are sure that the value will not be NULL
when set, you would be better served with isset()
. 假设您确定该值在设置时不会为
NULL
,那么使用isset()
会更好。 Something like 就像是
if( isset($jfo->years)
&& is_array($jfo->years)
&& isset($jfo->years[0]->year)
){
$mds = $jfo->years[0]->year;
}else{
$mds = 'mds not found';
}
Or if you wanted to be less verbose and know for sure that $jfo->years
will be an array. 或者,如果你想减少冗长,并确保
$jfo->years
将是一个数组。
$mds = isset($jfo->years[0]->year) ? $jfo->years[0]->year : 'mds not found';
Or if you are running php>=7 and want to be even less verbose. 或者如果你正在运行php> = 7并且想要更加冗长。
$mds = $jfo->years[0]->year ?? 'mds not found';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.