如何从PHP中的json响应中通过键提取值
问题描述:
我正在使用getResponse api来获取有关订户的更新.
这是var_dump($result);
I'm using getResponse api for getting updated about subscribers.
This is what is printing after var_dump($result);
object(stdClass)#2 (1) {
["updated"]=>
int(1)
}
我如何提取/解码/编码结果以请求密钥:更新"并获取其值:1?
How do i extract / decode / encode the result to request the key: "update" and get it's value: 1 ?
谢谢
答
// json object.
$contents = '{"firstName":"John", "lastName":"Doe"}';
// Option 1: through the use of an array.
$jsonArray = json_decode($contents,true);
$key = "firstName";
$firstName = $jsonArray[$key];
// Option 2: through the use of an object.
$jsonObj = json_decode($contents);
$firstName = $jsonObj->$key;