I have a JSON file that is being used to store settings, and a php file, which reads and populates the file according to the reported data, however after the settings are often edited by the JSON is has its format changed from Array to Object :
Required format:
{"favoritesIndex":[28,22,133,38,12,27]}
Generated format:
{"favoritesIndex":{"1":28,"2":22,"3":133,"4":38,"5":12,"6":27}}
PHP code:
function read() {return json_decode(file_get_contents('sec_settings/datatable.json'));}
function write($data){return file_put_contents('sec_settings/datatable.json',json_encode($data));}
function setFavorite()
{
$settings = read();
$settings->favoritesIndex[] = (int) $_REQUEST['id'];
$settings = write($settings);
}
function removeFavorite()
{
$settings = read();
foreach($settings->favoritesIndex AS $key => $value)
if($_REQUEST['id'] == $value)
unset($settings->favoritesIndex[$key]);
$settings = write($settings);
}
try
{
if(isset($_REQUEST['fn'])) $_REQUEST['fn']();
}
catch(Exception $e)
{
die("Could not process requested operation, {$e}");
}