Phuket IT

Computer => Web Site => Topic started by: aegkaluk on 26, 11, 2012, 12:09:47

Title: Handling JSON like a boss in PHP
Post by: aegkaluk on 26, 11, 2012, 12:09:47
Ref: http://nitschinger.at/Handling-JSON-like-a-boss-in-PHP

Code: [Select]
<?php
// Returns: ["Apple","Banana","Pear"]
json_encode(array("Apple""Banana""Pear"));
 
// Returns: {"4":"four","8":"eight"}
json_encode(array(=> "four"=> "eight"));
 
// Returns: {"apples":true,"bananas":null}
json_encode(array("apples" => true"bananas" => null));
?>

Code: [Select]
<?php
$string 
'{"foo": "bar", "cool": "attr"}';
$result json_decode($string);
 
// Result: object(stdClass)#1 (2) { ["foo"]=> string(3) "bar" ["cool"]=> string(4) "attr" }
var_dump($result);
 
// Prints "bar"
echo $result->foo;
 
// Prints "attr"
echo $result->cool;
?>