首页 > 编程知识 正文

在php中读取json数组(php遍历json)

时间:2023-12-14 13:33:53 阅读:315686 作者:VKTP

本文目录一览:

php提取json数据

$dataJson = "数据"; // 提供数据

$data = json_decode($dataJson); // 数据按json解析成php数组

$minuteArr = $data['minute']; // 从数组中获取minute数据

此时$minuteArr中存放的就是你要的数据,但数组下标(键)是从0开始,如需要从1开始,则遍历$minuteArr修改下标(键) 或 使用php数组函数进行快速重构。

注:你这问题是还没入门的初学者问的问题,建议还是多看看书!...

PHP json数据写入到json文件,读取json文件

// 生成一个PHP数组

$data = array();

$data['name'] = 'admin';

$data['pwd'] = '123456';

// 把PHP数组转成JSON字符串

$json_string = json_encode($data);

// 写入文件

file_put_contents('user.json', $json_string);

// 从文件中读取数据到PHP变量

$json_string = file_get_contents('user.json');

// 把JSON字符串转成PHP数组

$data = json_decode($json_string, true);

// 显示出来看看

var_dump($data)

php如何读取json的内容

如果json是以对象的方式存储于变量$json中,则这样引用: $json-openid

如果json是以关联数组的方式存储于变量$json中,则这样引用: $json['openid']

如果json是以字符串的方式存储于变量$json中,则需要先转换一下:

$a = json_decode($json);

print_r($a-openid);

或者

$a = json_decode($json,true);

print_r($a['openid']);

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。