更新JSON对象PHP

更新JSON对象PHP,php,Php,我想使用PHP根据用户输入更新JSON对象, 我的JSON文件如下所示: [ { id=1, text= "Sam", }, { id=2, text= "Jack", } ] 我想用用户输入的内容更新文本,请检查您的json格式,因为它无效。这是我的代码,希望对你有所帮助 //your input $id = 1; $input = "john"; //decode your json

我想使用PHP根据用户输入更新JSON对象, 我的JSON文件如下所示:

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

我想用用户输入的内容更新文本,请检查您的json格式,因为它无效。这是我的代码,希望对你有所帮助

//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
    }
}
然后您可以返回信息以显示更新

return $info;

用于将json字符串转换为PHP数组,然后对其进行更新。可能与的重复。这是否回答了您的问题?您将更新哪个文本,id=1或2,您如何知道哪个文本?