0

Hi I'm looking how to delete a array in json file based on the ID value.

Here is my json file

[
    {
        date: "3/7/2017",
        title: "kjjnkjnjn",
        category: "Automobile",
        ville: "Labas",
        decription: "nlknlknklnkjlnkl",
        mail: "mathieufrobert@yahoo.fr",
        telephone: "0777878988",
        pseudo: "kknklnkln",
        prix: "10",
        mdp: "0000",
        id: "5959785fde3bc26610"
    },
    {
        date: "3/7/2017",
        title: "kjjnkjnjn",
        category: "Automobile",
        ville: "Labas",
        decription: "nlknlknklnkjlnkl",
        mail: "mathieufrobert@yahoo.fr",
        telephone: "0777878988",
        pseudo: "kknklnkln",
        prix: "10",
        mdp: "0000",
        id: "6848675fde3bc26610"
    },{
        date: "3/7/2017",
        title: "kjjnkjnjn",
        category: "Automobile",
        ville: "Labas",
        decription: "nlknlknklnkjlnkl",
        mail: "mathieufrobert@yahoo.fr",
        telephone: "0777878988",
        pseudo: "kknklnkln",
        prix: "10",
        mdp: "0000",
        id: "5737554fde3bc26610"
    }
]

Here is my php file (it's a copy from another php)

<?php
$file = $_POST["id"];

$animals = file_get_contents('text.json');

$animals = json_decode($animals, true);
foreach ($animals as $key => $value) {
    if (in_array($file, $value)) {
        unset($animals[$key]);
    }
}
$animals = json_encode($animals);
?>

And my Ajax code

$("#5959785fde3bc26610").click(function() {
    $.ajax({
        url: "delete.php",
        type: "POST",
        data: {
            id: "5959785fde3bc26610",
        },
        success: function (result) {
            Materialize.toast('Annonce Supprimé!', 4000);
        }
    });
});

This is how I'd like my json file to look like

[
    {
        date: "3/7/2017",
        title: "kjjnkjnjn",
        category: "Automobile",
        ville: "Labas",
        decription: "nlknlknklnkjlnkl",
        mail: "mathieufrobert@yahoo.fr",
        telephone: "0777878988",
        pseudo: "kknklnkln",
        prix: "10",
        mdp: "0000",
        id: "6848675fde3bc26610"
    },{
        date: "3/7/2017",
        title: "kjjnkjnjn",
        category: "Automobile",
        ville: "Labas",
        decription: "nlknlknklnkjlnkl",
        mail: "mathieufrobert@yahoo.fr",
        telephone: "0777878988",
        pseudo: "kknklnkln",
        prix: "10",
        mdp: "0000",
        id: "5737554fde3bc26610"
    }
]

Thanks in advance for you're help.

Frank M
  • 170
  • 3
  • 14
Michael HEYRAUD
  • 57
  • 1
  • 10
  • which part of the code you need help? the php part or javascript ? – Long Kim Jul 02 '17 at 23:45
  • Javascript and PHP because it just doesn't work I may be missing something – Michael HEYRAUD Jul 02 '17 at 23:48
  • if that is the exact json in your file, i would say the first step would be formating it to have a right json format . eg : date, title. mail should be wrapped with double quote. You can check the validation of your json here [json formatter](https://jsonformatter.curiousconcept.com/) – Long Kim Jul 02 '17 at 23:52
  • 1
    Your JSON File is NOT VALID JSON – RiggsFolly Jul 03 '17 at 00:11

2 Answers2

2

First you would read the JSON in PHP, like this:

<?php

$id   = $_POST['id'];

// stop if it's non-numeric
if (!preg_match('/^\d+$/', $id)) {
  die('Don’t hack us.');
}

$file = 'the.json';
$data = file_get_contents($file);
$list = json_decode($data);

// now, we will search the ID
for ($i = 0; $i < count($list); $i++) {

  // if we found it,
  if ($list[$i]->id === $id) {
    // we remove it
    unset($list[$i]);
    break;
  }
}

// make the numeric array consecutive again
$list = array_values($list);

// write the resulting JSON to disk
$fp = fopen($file, 'w');
fwrite($fp, json_encode($list));
fclose($fp);
mynetx
  • 878
  • 7
  • 25
  • Thank you this worked for me =) How can I remove the count in front of the other array when it's deleted ? Thanks – Michael HEYRAUD Jul 03 '17 at 00:53
  • Which count do you mean? – mynetx Jul 03 '17 at 13:59
  • My file is like this : [{"date":"2017, 07, 01","title":"zeezerzrez"},{"date":"2017, 07, 03","title":"zeezerzrez"}] But after I use you're code my file look like this : [1:{"date":"2017, 07, 01","title":"zeezerzrez"},2:{"date":"2017, 07, 03","title":"zeezerzrez"}] There is a number in front and I can't use foreach code in ajax get – Michael HEYRAUD Jul 03 '17 at 14:24
  • I have amended my code to fix this. See the line commented with `// make the numeric array consecutive again`. The reason is documented in here: https://stackoverflow.com/questions/11722059/php-array-to-json-array-using-json-encode – mynetx Jul 03 '17 at 18:00
-2

I suggest you use ES6 Array.prototype.filter.

const deletePropWithId = (array, id) => {
  return array.filter((obj) => obj.id[id] !== undefined);
}
Reski
  • 169
  • 7