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


PHP数组数据写入JSON文件

<?php
// 生成一个PHP数组
$data = array();
$data['name'] = 'admin';
$data['pwd'] = '123456';
 
// 把PHP数组转成JSON字符串
$json_string = json_encode($data);
 
// 写入文件
file_put_contents('user.json', $json_string);
?>

JSON文件数据读取到PHP变量

<?php
// 从文件中读取数据到PHP变量
$json_string = file_get_contents('user.json');
 
// 把JSON字符串转成PHP数组
$data = json_decode($json_string, true);
 
// 显示出来看看
var_dump($data);
?>

声明:小小博客|版权所有,违者必究|如未注明,均为原创|本网站采用BY-NC-SA协议进行授权

转载:转载请注明原文链接 - PHP语言json数据写入到json文件,读取json文件


Carpe Diem and Do what I like