PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, May 16, 2022

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

 May 16, 2022     json, php     No comments   

Issue

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


Solution

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


Answered By - reshetech
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing