在PHP中,可以使用json_decode()
函数将JSON数据转换为数组。
$jsonData = '{"name": "John", "age": 30, "city": "New York"}';
$arrayData = json_decode($jsonData, true);
print_r($arrayData);
输出结果:
Array
(
[name] => John
[age] => 30
[city] => New York
)
在json_decode()
函数中,第一个参数是要解码的JSON数据,第二个参数设置为true
可以将JSON数据解码为关联数组(即使用字符串作为键名),不设置或设置为false
则解码为对象。
如果要将JSON数据解码为对象而不是数组,可以使用以下代码:
$jsonData = '{"name": "John", "age": 30, "city": "New York"}';
$objectData = json_decode($jsonData);
print_r($objectData);
输出结果:
stdClass Object
(
[name] => John
[age] => 30
[city] => New York
)
上一篇:php如何获取数组最小的只
下一篇:php 一维数组转二维数组
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站