0

I'm trying to select a dynamic method to remove key from a json file,at the moment, this code overwrite ALL json file and write {"0":{"name":null,"url":null}} do you know how can I solve it? I just want delete ONE key when I press delete button.

enter image description here

PHP:

<?php
var_dump($_POST);
$data_url = 'js/json.json';
$data_json = file_get_contents($data_url);
$data_array = json_decode($data_json, true);

$data[] =  array(
           'name' => $name,
           'url' => $url
    );

foreach($data as $key=>$val){
// check status
if ($val["status"]=="DELETE"){
// this deletes record from array
unset($data[$key]);
}
}

file_put_contents('js/json.json', json_encode($data, JSON_FORCE_OBJECT));
header('Location: http://URL/index.php');
?>

JSON:

[{"name":"asdf","url":"asdf"},{"name":"asfd","url":"dsaf"}]
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Joanmi
  • 410
  • 3
  • 17
  • where does the `$val["status"]` come from? It's not in the shown json... – Jeff May 24 '18 at 08:56
  • Your `$data` array doesn't even contain a 'status' key. You don't seem to be doing anything with your `$data_array` aswell... Provide us with some more information –  May 24 '18 at 08:56
  • the (corrected) code works as expected: https://3v4l.org/TYvVI – Jeff May 24 '18 at 08:58
  • Oh ok, this solved the problem about null but now, it erase all – Joanmi May 24 '18 at 08:58
  • @BrentLobbezoo Now I deleted the line if ($val["status"]=="DELETE") and I writed unset($data[$key]); but now it delete all – Joanmi May 24 '18 at 09:00
  • I assume that you get a URL through your post variables, but you don't use this in checking that your deleting the right record. – Nigel Ren May 24 '18 at 09:00
  • But I can't add "status" I just want when I click "delete", delete the value – Joanmi May 24 '18 at 09:01
  • 1
    Duplicate of your own question from only an hour earlier, [PHP Remove elements from json](https://stackoverflow.com/questions/50503234/php-remove-elements-from-json) - you really need to stop asking about the same topic multiple times, you have _four_ questions right now from within the last 24 hours that deal with this. – CBroe May 24 '18 at 09:13
  • @CBroe its not exactly the same but ok, I'll delete the posts – Joanmi May 24 '18 at 09:16

1 Answers1

1

You are reading the data into $data_array and writing out $data, so what you are writing is the data you want to delete - after you've processed it to delete all the records.

What this code does is take the $url value (from however it's set) and goes through the $data_array read from the input file and if it finds a match, it removes it. Then writes out $data_array back to the file.

$data_url = 'js/json.json';
$data_json = file_get_contents($data_url);
$data_array = json_decode($data_json, true);

// Next line just for testing
//$url = "asdf";    //  $_POST['URL']; ?
foreach($data_array as $key=>$val){
    // check URL
    if ($url == $val["url"]){
        // this deletes record from array
        unset($data_array[$key]);
    }
}
$data_array = array_values($data_array);
file_put_contents($data_url, json_encode($data_array));
Nigel Ren
  • 51,875
  • 11
  • 34
  • 49
  • Hmm now I tried your code, there are a problem, the keys are not introduced within [] and I can't assign "1":{} i've to change my js if I do it – Joanmi May 24 '18 at 09:12
  • Ive updated the code to add the line `$data_array = array_values($data_array);`, try this. – Nigel Ren May 24 '18 at 09:18
  • Ok, now we are nearly thanks you, but the last thing, the items that are not deleted change its format and transformated to (example) {"0":{"name":"aaaaaaaaaaa","url":"aaaaaaaaaaaaa"}} and it have to be [{"name":"aaaaaaaaaaa","url":"aaaaaaaaaaaaa"},{"name":"bbbbbbbbbb","url":"bbbbbbbbbbbb"}] – Joanmi May 24 '18 at 09:24
  • No I test it another time and it don't delete the "key" it change the format [{"name":"aaaaaaaaaaa","url":"aaaaaaaaaaaaa"},{"name":"bbbbbbbbbb","url":"bbbbbbbbbbbb"}] to {"0":{"name":"aaaaaaaaaaa","url":"aaaaaaaaaaaaa"}} and don't delete – Joanmi May 24 '18 at 09:26
  • Are you sure you don't have `, JSON_FORCE_OBJECT` on the `json_encode()`, that is the only way I can reproduce your problem – Nigel Ren May 24 '18 at 09:31
  • Oh yes I've it, Have I to delete it? – Joanmi May 24 '18 at 09:32
  • It should be as in the code in the answer (i.e. `json_encode($data_array)`) – Nigel Ren May 24 '18 at 09:32
  • Now I copy and paste your code and its not deleting nothing – Joanmi May 24 '18 at 09:35
  • Check your value for `$url` and also check for errors being reported (https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – Nigel Ren May 24 '18 at 09:38
  • Ohhh okey, now I understand but look the image on my post, I cant set $url to delete all time, it have to bee click and delete – Joanmi May 24 '18 at 09:40