-1

I want to update my JSON object according to user input using PHP , my JSON file looks like this :

[
  {
    id=1,
    text= "Sam",
  },
  {
   id=2,
    text= "Jack",
  }
]

I want to update the text with what the user will input

4

1 Answer 1

2

Kindly check your json format because it is not valid. This is my code, hope this will help you.

//your input
$id = 1;
$input = "john";

//decode your json
$info = json_decode($json);
foreach ($info as $data) {
    if ($data->id == $id) {   //find the id
        $data->text = $input; //update json object text depends on the user input
    }
}

then you can return the info to display update

return $info;
Sign up to request clarification or add additional context in comments.

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.