PHP,json_decode()参数true的用法

$a['d'][]=1;
$a['d'][]=2;
echo $str=json_encode(array($a));

var_dump(json_decode($str));
/*
array(1) {
  [0]=>
  object(stdClass)#1 (1) {
    ["d"]=>
    array(2) {
      [0]=>
      int(1)
      [1]=>
      int(2)
    }
  }
}
*/

var_dump(json_decode($str,true));
/*
array(1) {
  [0]=>
  array(1) {
    ["d"]=>
    array(2) {
      [0]=>
      int(1)
      [1]=>
      int(2)
    }
  }
}
*/

可以看出 json_decode($ d a t a , t r u e ) 输 出 的 一 个 关 联 数 组 , 由 此 可 知 j s o n d e c o d e ( data, true)输出的一个关联数组,由此可知json_decode( data,true),jsondecode(data)输出的是对象,而json_decode("$arr",true)是把它强制生成PHP关联数组。

json_encode()和json_decode()是编译和反编译过程,注意json只接受utf-8编码的字符,所以json_encode()的参数必须是utf-8编码,否则会得到空字符或者null。

你可能感兴趣的:(PHP,php)