一二三四网
您的当前位置:首页php如何将json字符串转为数组

php如何将json字符串转为数组

来源:一二三四网


php将json字符串转为数组的方法:可以利用json_decode函数来实现,如【json_decode($json, true);】。json_decode函数可以接受一个json格式的字符串并把它转换为php变量。

函数介绍:

(推荐教程:php视频教程)

json_decode接受一个JSON格式的字符串并且把它转换为PHP变量 ,当该参数$assoc为TRUE时,将返回array,否则返回object。

语法格式:

json_decode(string $json[,bool $assoc = false[,int $depth = 512[,int $options = 0]]])

代码实现:

<?php 
$json = '{"a":"php","b":"mysql","c":3}'; 
$json_Class=json_decode($json); 
$json_Array=json_decode($json, true); 
print_r($json_Class); 
print_r($json_Array); 
?>

输出结果:

stdClass Object (
  [a] => php
  [b] => mysql
  [c] => 3 )
  Array (
  [a] => php
  [b] => mysql
  [c] => 3 )
显示全文