json_encode() 与 json_decode();
函数 描述
json_encode 对变量进行 JSON 编码
json_decode 对 JSON 格式的字符串进行解码,转换为 PHP 变量
json_last_error 返回最后发生的错误
<?php
/**
* JSON format //json数据类型
* 数组:1.[1,2,3] //普通数组
* 2.[1,2,3,"hello","world] //混合类型数组
* 3.[1.2.3."hello","world",[4,5,6,]] //数组中含有数组
* 4.[1.2.3."hello","world",[4,5,6,],{"h":"hello"}] //数组中含有数组与对象
*
* 对象:1.{"H":"hello"} //普通对象
* 2.{"H":"hello",[1.2.3]} //含有数组的对象
*/
//encode >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //其他类型转JSON类型
$arr= array(1,2,3,"hello",array("h"=>"hello")); //1
print_r($arr);
echo "<br><br>";
echo json_encode($arr);
echo "<br><br>";
$obj=array("h"=>"hello",array(1,2,3)); //2
print_r($obj);
echo "<br><br>";
echo json_encode($obj);
echo "<br><br>";
//decode >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //JSON类型转其他类型
$jsonStr = '{"h":"hello","0":[1,2,3]}';
$abc = json_decode($jsonStr);
print_r($abc);
输出:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => hello [4] => Array ( [h] => hello ) )
[1,2,3,"hello",{"h":"hello"}]
Array ( [h] => hello [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) )
{"h":"hello","0":[1,2,3]}
stdClass Object ( [h] => hello [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) )
网友评论