用PHP更新Json值
我正在使用PHP启用和禁用一个值,该值存储在模块的config.json文件中。配置文件的格式为用PHP更新Json值,php,json,Php,Json,我正在使用PHP启用和禁用一个值,该值存储在模块的config.json文件中。配置文件的格式为 { "details": { "name": "Doxramos Core Login", "root": "index.inc", "language": "en_US", "ident": "dxcl", "version": "1.0", "author" : "Doxramos Development", "date" : "5/3
{
"details": {
"name": "Doxramos Core Login",
"root": "index.inc",
"language": "en_US",
"ident": "dxcl",
"version": "1.0",
"author" : "Doxramos Development",
"date" : "5/31/2016",
"module_url" : "allthingscode.net",
"author_url" : "allthingscode.net",
"core": true,
"version_tested": 1.0
},
"options": {
"location": "right_well",
"enabled": true
}
}
对于我的PHP函数,我让它运行
function ToggleModule($status, $configFile) {
$string = file_get_contents($configFile);
$json_a = json_decode($string, true);
$json_a['options']['enabled'] = $status;
}
这样,所有参数都能成功通过;
$json\u a['options']['enabled']
值由post变量控制,但我不知道如何在以后保存文件。试试这个。它应该对你有帮助。这将创建新的.json文件
$fp = fopen('new.json', 'w');
fwrite($fp, json_encode($json_a));
fclose($fp);
json\u decode
的反面是json\u encode
,而file\u get\u contents
的反面是file\u put\u contents
所以file\u put\u contents($configFile,json\u encode($json\u a))
应该可以工作。不幸的是不行。不过我很感激。它会出错吗?没有错误。我真的不知道如何从中获得var转储。在php中编辑文件对我来说是新事物。如果你想让我更新数据库,我已经有了,但这都是新的理由。请尝试var\u dump([“before”=>$string,“before”=>json\u encode($json\u a)])
比较前后状态。把它作为你函数的最后一个语句。答案很好,但不是我最后使用的那个。非常感谢。