简体   繁体   中英

Update Json Value with PHP

I'm working on enabling and disabling a value with PHP that's stored with a config.json file I have for my modules. The format of the config file is

{
  "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
  }
}

For my PHP Function I have it run

function ToggleModule($status, $configFile) {
    $string = file_get_contents($configFile);
    $json_a = json_decode($string, true);
    $json_a['options']['enabled'] = $status;
}

So with this all the parameters are passed successfully; the $json_a['options']['enabled'] value is controlled by the post variable, but I don't know how to get the file to save afterwards.

Try this. It should help you. This will create new.json file

$fp = fopen('new.json', 'w');
fwrite($fp, json_encode($json_a));
fclose($fp);

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