zuichumx0826

json在php中的使用之如何转换json为数组

复制代码
<?php
   $json = \'{"a":1,"b":2,"c":3,"d":4,"e":5}\';

   var_dump(json_decode($json));
   echo"<br/>";
   var_dump(json_decode($json, true));
?>
复制代码

数组$json = \'{"a":1,"b":2,"c":3,"d":4,"e":5}\';被json_decode()解码,转换回来的数据是对象,var_dump(json_decode($json))得到的是一个对象,如下:

1
object(stdClass)#1 (5) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) ["d"]=> int(4) ["e"]=> int(5) }

  那么,要怎么样才能把json数组转换为php格式的数组呢,采用以下方式:

json_decode($json, true)

    这样得到的数据就是php的数组了:

1
var_dump(json_decode($json, true));

  效果如下:

array(5) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) ["d"]=> int(4) ["e"]=> int(5) }

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-24
  • 2021-12-15
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-08
  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-03
相关资源
相似解决方案