【问题标题】:Update JSON Object PHP更新 JSON 对象 PHP
【发布时间】:2020-07-20 01:37:05
【问题描述】:

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

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

我想用用户输入的内容更新文本

【问题讨论】:

标签: php


【解决方案1】:

请检查您的 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;

【讨论】:

    猜你喜欢
    • 2019-02-11
    • 2017-10-21
    • 2021-10-09
    • 2012-01-31
    • 1970-01-01
    • 2023-02-16
    • 1970-01-01
    • 2017-12-21
    相关资源
    最近更新 更多