首页 > php数组 > php 利用json_decode强制json数据转换成数组

php 利用json_decode强制json数据转换成数组

一篇php 利用json_decode强制json数据转换成数组的简单应用实例参考文档,我们利用了var_dump(json_decode($str,true)); 就把json转换成我们想要的数据了,代码如下:

$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) 
} 
  } 
}

看到了吧这是一个数组里面放置一个对象,我们强制json_decode结果转换为数组吧——把第四行加上参数,代码如下:

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

   


本文地址:http://www.phprm.com/shuzu/fs5608.html

转载随意,但请附上文章地址:-)

标签:json_decode json数据转换数组

相关文章

发表留言