PHP用键删除整个JSON对象

时间:2019-06-25 07:50:35

标签: php html json

我的Json的结构如下


{
  "packages": [
  {
      "id": "TEST",
      "name": "TEST",
      "desc": "TEST",
      "image": "TEST",
      "package": "TEST",
      "version": "1.00",
      "picpath": "TEST",
      "desc_1": "TEST",
      "desc_2": "TEST",
      "ReviewStars": "TEST",
      "Size": "",
      "Author": "TEST",
      "apptype": "TEST",
      "pv": "TEST",
      "main_icon_path": "TEST",
      "main_menu_pic": "TEST",
      "releaseddate": "5/03/2018"
    },
{
    "id": "TEST",
    "name": "TEST",
    "desc": "TEST",
    "image": "TEST",
    "package": "TEST",
    "version": "1.00",
    "picpath": "TEST",
    "desc_1": "ssss",
    "desc_2": "ssss",
    "ReviewStars": "5",
    "Size": "TEST",
    "Author": "TEST",
    "apptype": "222",
    "pv": "TEST",
    "main_icon_path": "TEST",
    "main_menu_pic": "TEST",
    "releaseddate": "2019-06-19",
}

    ]
}


,并想删除数字为[0-x]的整个对象,所以如果我要删除1,它将看起来像这样



{
  "packages": [
  {
      "id": "TEST",
      "name": "TEST",
      "desc": "TEST",
      "image": "TEST",
      "package": "TEST",
      "version": "1.00",
      "picpath": "TEST",
      "desc_1": "TEST",
      "desc_2": "TEST",
      "ReviewStars": "TEST",
      "Size": "",
      "Author": "TEST",
      "apptype": "TEST",
      "pv": "TEST",
      "main_icon_path": "TEST",
      "main_menu_pic": "TEST",
      "releaseddate": "5/03/2018"
    }

    ]
}


我尝试过,但是它离开了[],仍然存在

$jsons = @file_get_contents($page);

$data = json_decode($jsons ,true);

unset($data['packages'][$pos] ['name']);
unset($data['packages'][$pos] ['id']);
unset($data['packages'][$pos] ['image']);
unset($data['packages'][$pos] ['desc']);
unset($data['packages'][$pos] ['version']);
unset($data['packages'][$pos] ['picpath']);
unset($data['packages'][$pos] ['package']);
unset($data['packages'][$pos] ['desc_1']);
unset($data['packages'][$pos] ['desc_2']);
unset($data['packages'][$pos] ['ReviewStars']);
unset($data['packages'][$pos] ['Size']);
unset($data['packages'][$pos] ['Author']);
unset($data['packages'][$pos] ['apptype']);
unset($data['packages'][$pos] ['pv']);
unset($data['packages'][$pos] ['main_icon_path']);
unset($data['packages'][$pos] ['main_menu_pic']);
unset($data['packages'][$pos] ['releaseddate']);
//unset($data['packages'][$pos]);


$newJsonStrings = json_encode($data, JSON_UNESCAPED_SLASHES);
file_put_contents($path, $newJsonStrings);

我的JSON在创建新对象之前只有0-7的对象,因此按数字进行操作将是最简单的方法iv还尝试通过havnt很好地搜索答案,我还尝试了其他解决方案,最终导致删除整个json大声笑

2 个答案:

答案 0 :(得分:1)

您可以将JSON转换为数组,并使用函数通过键删除子数组

$jsonToArray = json_decode($json, true);

removeByKey($jsonToArray['packages'] , 0);

function removeByKey(&$a, $key){
  unset($a[$key]);
  return $a;
}

以下功能可通过提供键数组和单个键来删除子数组

function removeByKey(&$a, $keys){
  if(is_array($keys)){
    foreach ($keys as $v) {
        if(isset($a[$v])) unset($a[$v]);
    }
  }else{
    if(isset($a[$keys])) unset($a[$keys]);
  }
  return $a; 
}

用法:-

$keys = [0,1];// array of keys
removeByKey($jsonToArray['packages'] , $keys);

https://3v4l.org/6FHGI

答案 1 :(得分:0)

在这种情况下,您只需要使用unset($data['packages'][$pos])