encode()和decode()区别

2016-11-13  本文已影响0人  左耳击水兽

一、json_encode()

<?php$arr = array ('a'=>1,'b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);?>
{"a":1,"b":2,"c":3,"d":4,"e":5}
$obj->body           = 'another post';
$obj->id             = 21;
$obj->approved       = true;
$obj->favorite_count = 1;
$obj->status         = NULL;
echo json_encode($obj);
{
   "body":"another post",
   "id":21,
   "approved":true,
   "favorite_count":1,
   "status":null
 }

二、json_decode()

$json = '{"foo": 12345}';
$obj = json_decode($json);
print $obj->{'foo'}; // 12345
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
object(stdClass)#1 (5) {
 
  ["a"] => int(1)
  ["b"] => int(2)
  ["c"] => int(3)
  ["d"] => int(4)
  ["e"] => int(5)
 
}
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}'; 
var_dump(json_decode($json,true));
array(5) {   ["a"] => int(1)  
                      ["b"] => int(2)  
                      ["c"] => int(3)   
                          ["d"] => int(4)  
                       ["e"] => int(5)}
上一篇 下一篇

猜你喜欢

热点阅读