1

my Json structured like this


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

    ]
}


and want to delete whole objects by number [0-x] so if i would delete 1 it would look like this



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

    ]
}


i tried this but it leaves [], still there

$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);

my JSONs only have objects from 0-7 before it creates a new one so doing it by number would be easiest iv also tried googling the answer by havnt had much luck, i also tried doing other solutions that just end up delete the whole json lol

  • What do you mean by "it leaves []"? – Nico Haase Jun 25 '19 at 07:51
  • If you want to remove the whole object from the array, you should do exactly that: `unset($data['packages'][$pos]);`. – jeroen Jun 25 '19 at 07:55
  • 1
    in addition to @jeroen: After removing all the desired packages use `array_values` before json encoding to reset the packages indexes – Mihai Matei Jun 25 '19 at 07:57
  • as my predecessors mentoined you can unset the hole array entry or you can use array_splice - see the similar question and solution behind this link https://stackoverflow.com/questions/369602/deleting-an-element-from-an-array-in-php there is also a way to delete more than one element at once – J. Knabenschuh Jun 25 '19 at 08:04

2 Answers2

1

You can convert JSON to array and use a function to delete sub-array by key

$jsonToArray = json_decode($json, true);

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

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

Following function can be used to remove sub-array by supplying an array of keys and a single key

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

Usage :-

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

https://3v4l.org/6FHGI

Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
  • iv tried but instead of becoming this { "packages": [ { "id": ... it becomes this ``` { "packages": [ "0": { "id": which for my purposes i cant have it strrt numbering them – user11611146 Jun 25 '19 at 14:40
0

For this scenario, you just need to use unset($data['packages'][$pos])

Phuc Lam
  • 370
  • 2
  • 8
  • iv tried but instead of becoming this ``` { "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" } ] } – user11611146 Jun 25 '19 at 14:31
  • it becomes this ``` { "packages": [ "0": { "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" } ] } ``` – user11611146 Jun 25 '19 at 14:32