How to extract key value from json response in PHP

I am using the getResponse api to get subscriber updates. This is what prints aftervar_dump($result);

object(stdClass)#2 (1) {
  ["updated"]=>
  int(1)
}

How to extract / decode / encode the result to request a key: "update" and get its value: 1?

thank

+4
source share
2 answers
    // 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;

+5

, , json_decode - JSON stdClass, -, :

$string = '{"updated":1}';
$array = json_decode($string, true);
echo $array['updated'];

updated , :

$obj = json_decode($string);
echo $obj->updated;
+1

All Articles