在PHP中取消设置JSON的特定属性


Unset specific property of JSON in PHP

JSON

{
 "pages":{
   "index.php":{
      "status":"enabled",
      "theme":"dark",
      "identifier":"KMS"
  },
   "google.php":{
      "status":"enabled",
      "theme":"dark",
      "identifier":"KMS"
  },
   "doodle.php":{
      "status":"disabled",
      "theme":"light",
      "identifier":"transact"
   }
 }
}

在我的PHP代码中,我编写

$jsona = file_get_contents("../pages.json");
$jsonb = json_decode($jsona,true);  
$data = $jsonb['pages'];

现在,如果我想删除属性"index.php",我写unset($data["index.php"],然后写

file_put_contents("../pages.json",json_encode($data));

尽管在转到我的JSON文件后,它删除了"pages"

实际结果

{"google.php":{"status":"enabled","theme":"dark","identifier":"KMS"},"doodle.php":{"status":"disabled","theme":"light","identifier":"transact"}}

我只需要取消设置页面的特定子属性。例如CCD_ 4或CCD_。我检查了发布为$data[$page]的内容,它是特定的元素。那么,为什么要取消设置pages并保留其余属性呢?

不要将$data设置为$jsonb['pages']。这是专门将$data变量设置为整个对象的一个子部分。

只需使用unset($jsonb['pages']['index.php'])即可。