可以使用 PHP 内置函数 'json_decode' 将 JSON 字符串转换为 PHP 数组,示例代码如下:

$json_str = '{"name":"Tom","age":18}';
$array = json_decode($json_str, true);
echo '<pre>';
print_r($array);
echo '</pre>';

其中,'json_decode' 函数的第二个参数设置为 'true',表示要将 JSON 字符串转换为关联数组。输出结果如下:

Array
(
    [name] => Tom
    [age] => 18
)

需要注意的是,如果 JSON 字符串格式不正确或者无法解析,则 'json_decode' 函数会返回 'null'。

PHP 将 JSON 字符串转换为数组并输出 - 示例代码

原文地址: https://www.cveoy.top/t/topic/nzh7 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录