php中怎么把json转成对象数组-PHP问题

资源魔 21 0

php中怎样把json转成工具数组 ?

正在php中能够应用json_decode函数将json转成工具数组。

json_decode是php5.2.0之后新增的一个PHP内置函数,其作用是对JSON格局的字符串进行编码.

那末这个函数该若何应用呢?

json_decode的语法例则:

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

json_decode承受一个JSON格局的字符串而且把它转换为PHP变量 ,当该参数$assoc为TRUE时,将前往array,不然前往object。

JSON 格局的字符串

$json = '{"a":"php","b":"mysql","c":3}';

此中a为键,php为a的键值。

实例:

<?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 )

正在下面代码的条件下

拜访工具类型$json_Class的a的值

echo $json_Class->{'a'};

顺序输入:php

拜访数组类型$json_Array的a的值

echo $json_Array['a'];

顺序输入:php

保举:《php教程》

以上就是php中怎样把json转成工具数组的具体内容,更多请存眷资源魔其它相干文章!

标签: php php教程 php故障解决 php使用问题

抱歉,评论功能暂时关闭!