[英]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. 这段代码试图删除索引$ entry的JSON子项,该子项以数字形式传递。 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. 将第二个参数传递为true将返回您使用的数组。
$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: 如果您不愿意将第二个参数用作true,那么您将获得对象。在这种情况下,您需要这样访问:
$json->{$entry}
I think you are unsetting a variable not set at all. 我认为您正在取消一个根本没有设置的变量。
May be $json
is not getting value. 可能是
$json
没有得到价值。
Do this: 做这个:
$json = json_decode(file_get_contents($file));
if (! empty($json[$entry])) {
unset($json[$entry]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.