json_decode
对 JSON 格式的字符串进行解码
PHP 5.2.0及以上版本
json_decode() 函数用于将一个 JSON 格式的字符串转换为 PHP 变量。
json_decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0): mixed
返回 PHP 变量,具体类型由解码的 JSON 数据决定。如果解析失败,返回 NULL。
假设你有一个 JSON 字符串,你想将它解码为 PHP 数组:
$jsonString = '{"name": "John", "age": 30, "city": "New York"}';
$decodedArray = json_decode($jsonString, true);
var_dump($decodedArray);
上述代码将 JSON 字符串解码为 PHP 关联数组,输出结果将会是:
array(3) {
["name"] => string(4) "John"
["age"] => int(30)
["city"] => string(8) "New York"
}