PHP 将 JSON 字符串转换为可输出的数组
PHP 将 JSON 字符串转换为可输出的数组
可以使用 json_decode() 函数将 JSON 字符串转换为 PHP 数组,然后使用 print_r() 函数将数组转换为可输出的字符串格式。
示例代码如下:
$json_str = '{"name":"John", "age":30, "city":"New York"}';
$arr = json_decode($json_str, true);
echo '<pre>';
print_r($arr);
echo '</pre>';
输出结果:
Array
(
[name] => John
[age] => 30
[city] => New York
)
说明:
json_decode()函数将 JSON 字符串转换为 PHP 数组。第二个参数true表示将 JSON 字符串转换为关联数组。print_r()函数输出数组的内容,并以可读的格式显示。<pre>标签用于保留空格和换行符,使输出结果更易于阅读。
原文地址: https://www.cveoy.top/t/topic/nzhH 著作权归作者所有。请勿转载和采集!