更新json替换php中来自表单的变量输入

时间:2018-09-26 07:05:01

标签: php json

问候,我是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();
}

我想念什么吗?

2 个答案:

答案 0 :(得分:1)

尝试我的代码。

 private void UpdateData() {
                              u.setOnClickListener(
                                      new View.OnClickListener() {
                                          @Override
                                          public void onClick(View v) {
                                              uptable.setVisibility(View.VISIBLE);
                                              again.setVisibility(View.VISIBLE);

                            Cursor res = mydManager.selectRow(text);


                            if (res != null && res.moveToFirst()) {
                                String id = Integer.toString(res.getInt(0));
                                String nme = res.getString(1);
                                String lnme = res.getString(2);
                                String gen = res.getString(3);
                                String corse = res.getString(4);
                                String ag = Integer.toString(res.getInt(5));
                                String lo = res.getString(6);
                                studid.setText(id);
                                fname.setText(nme);
                                lname.setText(lnme);
                                gender.setText(gen);
                                course.setText(corse);
                                age.setText(ag);
                                loc.setText(lo);

                            }


                        }
                    }
            );
        }


        public void UpdateData1() {
            again.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            uptable.setVisibility(View.GONE);
                            String id = studid.getText().toString();
                            String nme = fname.getText().toString();
                            String lnme = lname.getText().toString();
                            String gen = gender.getText().toString();
                            String corse = course.getText().toString();
                            String ag = age.getText().toString();
                            String lo = loc.getText().toString();
                            boolean isUpdated = mydManager.updateData(id, nme , lnme, gen, corse ,ag , lo);
                            if (isUpdated == true)
                                Toast.makeText(Main4Activity.this, "Data Updated", Toast.LENGTH_LONG).show();


                            else
                                Toast.makeText(Main4Activity.this, "Data Not Updated", Toast.LENGTH_LONG).show();
                        }
                    }
            );
        }

答案 1 :(得分:0)

正如您在评论中提到的,$json_arr的内容是:

{"CELL_NAME":"1234A","@25":"remark value"}

因此,当您尝试访问时:

 foreach ($json_arr as $key => $value) {
     if ($value['CELL_NAME'] == $_POST['in_cell']) {
         $json_arr[$key]['@25'] = $_POST['in_remark'];
     }
 }

$value键处没有CELL_NAME的键。

我想您文件中的$data应该是这样的(JSON数组):

$data = '[{"CELL_NAME":"1234A","@25":"remark value"}, {"CELL_NAME":"1234B","@25":"remark value"}]';

现在您可以执行此操作,它将起作用:

$arr = json_decode($data, true);
foreach ($arr as $key => $value) {
    if ($value['CELL_NAME'] == "1234A") // you can change with $_POST['in_cell']
        $arr[$key]['@25'] = "test"; // you can change with $_POST['in_remark']
}