[
{
"id" : 1,
"name" : "levin",
"description" : "some desc",
"size" : "100KG",
"actions" : {
"walking" : true,
"eating" : true
}
},
{
"id" : 2,
"name" : "clara",
"description" : "some desc",
"size" : "2000KG",
"actions" : {
"walking" : false,
"eating" : true
}
}
]
这是我的person.json文件。我喜欢更新(覆盖)现有的值。没有发现任何有关此问题的有用问题。如何使用php更新(覆盖)json文件
我有一个名字“levin”,我喜欢将它覆盖为空或“---”。但其应通过“ID”只根据。继是我的PHP代码,但它不工作:(
public function api_put()
{
//print($this->put('id')); am getting here 2 value from other page
//print($this->put('action'));.
if($this->put('action') == "remove"){
$file = json_decode(file_get_contents("assets/json/person.json"));
$new_val = array();
$i = 0 ;
foreach ($file as $key => $value) {
if((string)$value->id == $this->put('id')) {
$data[] = (string)$value->name="--";(string)$value->description="--";
$new_val[$i] = $data;
$i++;
}
}
file_put_contents('assets/json/person.json', json_encode($new_val));
$message = array('id' => $this->put('id'), 'message' => 'Successfully updated!!');
$this->response($message, 200);
}
}
我如何可以覆盖一个JSON值取决于特定的ID,并用了改变所有其它ID数据。我用笨REST API。在此先感谢
将文件更改为php,然后使用while循环来填充数据 – Afsar
你是什么意思你的代码不工作?你得到了什么输出,以及你的预期输出是什么? –
我喜欢用php –