Home  >  Article  >  php教程  >  php 利用json_decode强制json数据转换成数组

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

WBOY
WBOYOriginal
2016-06-13 11:19:20881browse

一篇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)
    }
  }
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn