doubi9531 2014-07-21 04:14
浏览 72
已采纳

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

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"
   }
 }
}

In my PHP code I write

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

Now if I want to delete the property "index.php" I write unset($data["index.php"] and then I write

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

Though after going to my JSON file it deletes "pages"

actual outcome

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

I need to just unset a specific child property of pages. Such as "google.php" or "doodle.php". I checked what is being posted as $data[$page] and it is the specific element. So why is it unsetting pages and leaving the rest of the properties?

  • 写回答

1条回答 默认 最新

  • doumou1864 2014-07-21 04:19
    关注

    Don't set $data to $jsonb['pages']. That's specifically setting your $data variable to a sub-section of your overall object.

    Just use unset($jsonb['pages']['index.php']).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑