要将 JSON 字符串转换为 PHP 数组,可以使用 json_decode() 函数。该函数将 JSON 字符串解码为 PHP 对象或数组。
以下是一个示例:
$jsonString = '{"name":"John", "age":30, "city":"New York"}';
$array = json_decode($jsonString, true);
print_r($array);
输出结果:
Array
(
[name] => John
[age] => 30
[city] => New York
)
在上面的示例中,json_decode() 函数的第二个参数设置为 true,表示将 JSON 对象解码为关联数组。如果不设置该参数或设置为 false,则解码为 PHP 对象。
如果要将 JSON 字符串解码为 PHP 对象,可以将 json_decode() 函数的第二个参数省略或设置为 false:
$jsonString = '{"name":"John", "age":30, "city":"New York"}';
$object = json_decode($jsonString);
print_r($object);
输出结果:
stdClass Object
(
[name] => John
[age] => 30
[city] => New York
)
上一篇:php+两个数组是否
下一篇:php怎么把变量变成数组
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站