Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. I check, compile and run every program. Contact me at: aviboots(AT)netvision.net.il

Hostgator - Save 50% Off Your New Optimized WordPress Hosting With Coupon: WDA50

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Dedicated Hosting Superb Internet

simplilearn big data and analytics

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Buy Cool Shirts

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


6,300 questions

8,938 answers

573 users

How to decode a JSON string into an object in PHP

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial


7 views
asked Sep 3 by avibootz

3 Answers

0 votes
$json_string = '{"name": "Tim", "age": 42, "profession": "Programmer"}';
$object = json_decode($json_string);

printf('%s %d %s', $object->name, $object->age, $object->profession);


/*
run: 
 
Tim 42 Programmer
 
*/

 




answered Sep 3 by avibootz
edited Sep 3 by avibootz
0 votes
$json_string = '{"name": "Tim", "profession": "Programmer", "languages": ["PHP", "JAVA"]}';
$object = json_decode($json_string);
 
printf('%s %s %s %s', $object->name, $object->profession, 
                      $object->languages[0], $object->languages[1]);


/*
run: 
 
Tim Programmer PHP JAVA
 
*/

 




answered Sep 3 by avibootz
0 votes
$json_string = '{"name": "Tim", "profession": "Programmer", "languages": ["PHP", "JAVA"]}';
$object = json_decode($json_string);
                      
echo "<pre>";
var_dump($object);
echo "</pre>";

/*
run: 
 
object(stdClass)#1 (3) {
  ["name"]=>
  string(3) "Tim"
  ["profession"]=>
  string(10) "Programmer"
  ["languages"]=>
  array(2) {
    [0]=>
    string(3) "PHP"
    [1]=>
    string(4) "JAVA"
  }
}
 
*/

 




answered Sep 3 by avibootz
...