[
{
"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 –