Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract value by key from json response in PHP [duplicate]

Tags:

json

php

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)
}

How do i extract / decode / encode the result to request the key: "update" and get it's value: 1 ?

Thanks

like image 396
user1610208 Avatar asked Nov 14 '14 15:11

user1610208


Video Answer


1 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;

like image 94
reshetech Avatar answered Oct 05 '22 10:10

reshetech