如何从PHP中的json响应中通过键提取值 [英] How to extract value by key from json response in PHP

查看:85
本文介绍了如何从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;

这篇关于如何从PHP中的json响应中通过键提取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆