简体   繁体   中英

php unset causing internal server error

function deleteThing() {

    if($_REQUEST ['entry'] == "") {
        exit;
    }

    $entry = $_REQUEST ['entry'];

    $file = 'entries.json';

    $json = json_decode(file_get_contents($file));

    unset($json[$entry]);

    file_put_contents($file, json_encode($json));


}

This code is trying to delete a JSON sub item at the index $entry which is passed as a number. I'm unsure if im using unset properly or not

it seems that you need to try like this:
passing second parameter as true will return array that you have used.

$json = json_decode(file_get_contents($file),true);//assign as  array
if(isset($json[$entry])) { //check if it is set
    unset($json[$entry]);
}

if you not willing to using second param as true then you will get object.In that case you need to access like this:

$json->{$entry}

I think you are unsetting a variable not set at all.

May be $json is not getting value.

Do this:

$json = json_decode(file_get_contents($file));
if (! empty($json[$entry])) {
  unset($json[$entry]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM