php怎么将json转换成数组

来自:互联网
时间:2021-03-22
阅读:

json_decode

(PHP 5 >= 5.2.0, PHP 7, PHP 8, PECL json >= 1.2.0)

json_decode — 对 JSON 格式的字符串进行解码

说明

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

接受一个 JSON 编码的字符串并且把它转换为 PHP 变量

参数

json

待解码的 json string 格式的字符串。

这个函数仅能处理 UTF-8 编码的数据。

注意:

PHP 实现了 JSON 的一个超集。

assoc

当该参数为 true 时,将返回 array 而非 object 。

depth

指定递归深度。

options

由 JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR 组成的掩码。 这些常量的行为在JSON constants页面有进一步描述。

返回值

通过恰当的 PHP 类型返回在 json 中编码的数据。值true, false 和 null 会相应地返回 true, false 和 null。 如果 json 无法被解码, 或者编码数据深度超过了递归限制的话,将会返回null 。

示例 #1 json_decode() 的例子

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>

以上例程会输出:

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