PHP 中解析 JSON 有两种方法:json_decode() 函数:将 JSON 字符串转换为 PHP 数组。json_parse() 函数:直接解析 JSON 字符串并返回解析后的数据,通常是一个关联数组。
如何解析 JSON
在 PHP 中,可以使用两种主要方法来解析 JSON:
1. json_decode() 函数
json_decode() 函数用于将 JSON 字符串转换为 PHP 数组。它采用一个 JSON 字符串作为参数并返回一个包含解析后的数据的数组。
立即学习“PHP免费学习笔记(深入)”;
语法:
$php_array = json_decode($json_string);
示例:
$json_string = '{"name": "John Doe", "age": 30, "city": "New York"}'; $php_array = json_decode($json_string); var_dump($php_array); // 输出:array(3) { ["name"]=> string(7) "John Doe" ["age"]=> int(30) ["city"]=> string(8) "New York" }
2. json_parse() 函数
json_parse() 函数是 JSON 解析的底层函数,它直接解析 JSON 字符串并返回解析后的数据。
语法:
$php_array = json_parse($json_string);
示例:
$json_string = '{"name": "John Doe", "age": 30, "city": "New York"}'; $php_array = json_parse($json_string); var_dump($php_array); // 输出:{ // "name": "John Doe", // "age": 30, // "city": "New York" // }
两者的区别
以上就是php 如何解析 json的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号