1

Greeting, I'm new to PHP, and currently looking for a way to edit json. I have a form to input the key that wants to edit. I echo the input from form and successfully got the output showing but it didn't pass to json file to update. The code is as below.

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();
}

Am I missing something?

11
  • seems like your code is okay. are you sure json file didn't change? Commented Sep 26, 2018 at 7:12
  • Can you share var_dump($_POST) ? Commented Sep 26, 2018 at 7:12
  • I don't manage to understand your exact problem. Those the testfun being called? can you please print the $json_arr before and after the for loop? Commented Sep 26, 2018 at 7:13
  • @david yes it didn't change Commented Sep 26, 2018 at 7:17
  • Can you please share example (short one) of the $json_arr? Commented Sep 26, 2018 at 7:20

2 Answers 2

1

Try my code.

 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();
}
Sign up to request clarification or add additional context in comments.

12 Comments

Foreach $json_arr because it's a one-dimensional array $value that only outputs the result directly, so no key value is required
What have you changed?
Change here $json_arr = array(json_decode($data, true));@user10417098
What? I didn't cast to array - I think his input is corrupted
You just need to change $json_arr = json_decode($data, true); of your code to $json_arr = array(json_decode($data, true)).
|
0

As you mention in the comments, the content of the $json_arr is:

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

So when you trying to access in:

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

it has no key for $value at CELL_NAME key.

I guess your $data from the file should be like that (an array of JSONs):

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

Now you can do this and it will work:

$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']
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.