温馨提示×

php如何将json转换成数组

小亿
94
2023-11-10 17:09:06
栏目: 编程语言

可以使用json_decode()函数将JSON字符串转换为PHP数组。该函数的用法如下:

$json = '{"name":"John", "age":30, "city":"New York"}';
$array = json_decode($json, true);

print_r($array);

输出结果为:

Array
(
    [name] => John
    [age] => 30
    [city] => New York
)

json_decode()函数中,第一个参数是要转换的JSON字符串,第二个参数设置为true可以将JSON对象转换为关联数组。如果不设置第二个参数或设置为false,则将JSON对象转换为PHP对象。

0