php - 更新json替换php中表单中的变量输入
问题描述
问候,我是 PHP 新手,目前正在寻找一种编辑 json 的方法。我有一个表格来输入要编辑的键。我从表单回显输入并成功显示输出,但它没有传递给 json 文件进行更新。代码如下。
function testfun()
{
// read file
$data = file_get_contents('Book2.json');
// decode json to array
$json_arr = json_decode($data, true);
foreach ($json_arr as $key => $value) {
if ($value['CELL_NAME'] == $_POST['in_cell']) {
$json_arr[$key]['@25'] = $_POST['in_remark'];
}
}
// encode array to json and save to file
file_put_contents('Book2.json', json_encode($json_arr));
}
//you this post contain test?
//test is the the button when i submit the form
if (array_key_exists('test',$_POST))
{
testfun();
}
我错过了什么吗?
解决方案
试试我的代码。
function testfun()
{
// read file
$data = file_get_contents('Book2.json');
// decode json to array
$json_arr = array(json_decode($data, true));
foreach ($json_arr as $key => $value) {
if ($value['CELL_NAME'] == $_POST['in_cell']) {
$json_arr[$key]['@25'] = $_POST['in_remark'];
}
}
// encode array to json and save to file
file_put_contents('Book2.json', json_encode($json_arr));
}
if (array_key_exists('test',$_POST))
{
testfun();
}
推荐阅读
- javascript - 如何用视图模型和表格内容序列化表单?
- flutter - 无法解析平台“iphoneos”的产品类型“com.apple.product-type.application.watchapp2”(目标“watchVersion”来自
- azure-devops - 组织所有者在 Azure DevOps 中拥有哪些权限?
- python - 如何添加像 Matrices Python/Pandas 这样的 DataFrame?
- ruby-on-rails - 无法访问 rubygems.org
- google-coral - 编写一个程序,该程序需要一年并确定该年是否是 Coral 的闰年
- amazon-web-services - Terraform 导入默认选择与提供程序代码块中设置的 AWS 配置文件不同的 AWS 配置文件
- data-structures - 数据模型的选择:要更新和映射的试剂原子的嵌套映射或向量?
- java - 将 GLOB_RECURSE 与 add_jar 一起使用
- reactjs - 为什么我的 Model.find().fetch() 在服务器端从 Mongodb 返回数据,然后在 Meteor 的客户端返回 [ ]?